From the course: CompTIA Linux+ (XK0-005) Cert Prep

What makes a shell script a shell script? - Linux Tutorial

From the course: CompTIA Linux+ (XK0-005) Cert Prep

What makes a shell script a shell script?

- [Instructor] The beauty of shell scripting is that we don't need a complex development environment to get started. To make a shell script that acts like a system command, we do need to do a few things. First, we create a text file in our favorite editor. In a terminal type in VI space tilde slash script dot SH, then hit enter. We've named it script.sh, so that VI will do syntax highlighting us. Scripts don't have to be named like this, but it makes it nicer for text editors. Syntax highlighting makes debugging easier. For the next step, we need to edit the text file and make the first line point to the shell interpreter. We can do this by pointing it directly at the interpreter using an absolute path. Go in to insert mode by pressing the I key and then add hash bang slash bin slash bash. In this case, we're specifying an absolute path to the bash interpreter executable. We can also use a second form. Change it to hash bang slash USR slash bin slash ENV space bash. In this case, we use the ENV command to search the system path for the bash interpreter and execute it there. The advantage is that it could be in a different location and it will still work. Let's add one line of text to our script. Hit enter and then type in echo space double quote. This is a shell script, double quote. And save and exit by pressing escape, colon, X, exclamation mark, and hitting enter. After we've added code and saved our script we need to change the permissions and make it executable. Type in, type in chmod space U plus X space, script.sh, and hit enter. This step allows us to execute our script as if it were a command. The last thing we would do to make it seamless is to put our command in the system path. In enterprise Linux seven, we have a directory set aside for this. It just hasn't been created yet. When a user logs in a directory is added to the system path specifically for that user. That directory is tilde slash bin. We need to create this directory first. Type in MKR space tilde slash bin, and hit enter. Lastly, we'll move our script into the bin directory. Type in MV space script.sh space tilde slash bin and hit enter. Now we can execute any script in this directory just by calling its name without a path. Type in script.sh and hit enter, and it runs as if it were a system command. You can name your scripts without the .sh extension to make them seem more like system commands, or even create symbolic links to them without the extension. Note that none of these steps are actually required to run a text file as a shell script. You can create a text file and provide it as an argument to bash and it will run. For instance, type echo space, single quote, echo space, this is a shell script, single quote, space, greater than, space tilde slash bin slash script two sh, and hit enter. This new script only has one line in it, that reads "echo this is a shell script." Now let's run it by providing it as an argument to bash. Type in bash space tilde slash bin slash script two .sh and hit enter, and it runs even without execute permissions or a line indicating the desired interpreter.

Contents