In previous articles we have covered the concept of scripting, and looked at how Bash has a number of built in features that can enable it to be used as a scripting language to automate tasks on your system. In the last part, we covered the shebang and script file permissions, so let’s look at some basic scripts that build upon that.
Hello World Script: Using Nano To Edit Files
Following Bash Scripts Part 1, we’ll start off with the most basic script: the hello world script. First, we’ll need to make the script file:
Note here that I’ll be using nano to edit the files, though you can use whichever text editor that your prefer. Inside this file we’ll paste the following code:
printf “Hello World!n”
Save and exit the file, then make it executable:
chmod +x hello.sh
Now run it with the following command:
You should now see the following output:
So, to explain what is going on here, the printf command will print to screen any text that is contained within the pair of speech marks following it. As you can see, the text “Hello World!” was printed to the screen when the script was run. However, the letters “n” were not. This is because “n” is a code to tell printf to insert a newline character. These are called escape sequences and consist of a backslash (the escaping character) and a following character that gives the sequence it’s meaning. Here are some common ones:
n A new line.
t A tab.
$ The dollar symbol, as the dollar symbol itself has special meaning in Bash.
\ A backslash symbol, as a single backslash is interpreted as the start of an escape sequence.
“ Speech mark, as the speech marks are normally interpreted as the start and end of a sequence of text to be echoed.
There are a number of other escape sequences, but these are some major ones to be aware of when getting started. The printf command is a common one to be used in a script as it is generally how you will communicate what your script is doing with the end user. It is also often used when text needs to be piped to another command.
Printf Vs Echo Command
Something to note is that you may see a number of scripts making use of the echo command in place of printf, a command that is often somewhat easier to understand in its meaning, but has the downside of inconsistent implementation across shells and systems. As an example, replacing printf with echo in the above script on Ubuntu provided the expected response of a newline being added, in CentOS the “n” characters weren’t properly converted to a newline but were printed as in the file. For this reason if you want your script to work on more than one Linux distribution or shell, it’s recommended that you use the printf command instead of echo.
Now let’s take the following code and put that into the hello.sh file, replacing the previous code:
printf “Hello World!nThis is $HOSTNAME.”
Save it, and then run the file as before and you should get an output similar to the below:
This is testing.hayden@testing:~$
You will see that the text $HOSTNAME has been replaced by the system hostname, in this case “testing”. This is because the dollar symbol is used to reference a variable within a script, and $HOSTNAME as a standard environment variable would be available for the script to read it’s contents. Environment variables are something we’ve covered on the 100TB blog recently and can be handy things to know and be able to reference when creating scripts. Bash will read the command from the line in the script and before executing it any strings starting with a dollar symbol will be interpreted to be variables, and will be replaced with the contents of that variable that is stored in memory.
A second important note is that the command prompt for the next command to be issued after the script was run appears on the same line as the last line of the script. This is because we didn’t include a newline escape sequence at the end of the printf command, so you’ll need to remember to explicitly include this when you want a new line to start.
That pretty much wraps up this part, but in the next one we’ll be looking into variables with a little more detail.