Bash Scripting for Beginners [Part 1] - Shebang and echo

Bash Scripting for Beginners [Part 1] - Shebang and echo

With bash scripts you automate tasks and create "little" programs in an Linux or Unix environment.

In this series you learn how to create bash scripts and learn about the technical background, which is important for understanding, how the bash is really working.

In this first part, you learn the basics, on how to create a script file, make it exectuable and work with the shebang. We also take a look at the echo command and on escaping.

Bash for Bourne Again shell is one of the most common shells for Linux and offers also an extended language for writing scripts.

Shebang #!

At the beginning of a script you define the interpretor. In case of the bash you start with #!/bin/bash or better #!/usr/bin/env bash to inform the opeating system, which interpretor should be used to run the file. Normally the # is interpreted as comment, the Shebang is only used if you execute the script directly without the interpretor.

This is not limited to the shell, you can basically set any interpretor, like Python 3 with #!/usr/bin/env python3.

env is normally used to print environment variables, but also resolves the path to the interpretor (including possible user overwrites). For example on different systems the path to the bash may not always be /bin/bash. By using env your script is going to be more portable.

So let's start with our first hello world script, in it we just use echo to display a text.

#!/usr/bin/env bash

echo "Hello World"

Just save it to a file called hello_world.sh. Now you can execute it by calling it with the interpretor directly, with bash hello_world.sh

The ending .sh is totally optional and is just an helper for you.

Calling a script with the bash application is not always very handy and if you plan on adding it to your $PATH you should make the file executable, by using chmod u+x hello_world.sh. Now the file is executable for your user. If you want to make it executable for all users, just use chmod +x hello_world.sh.

The PATH variable defines in which folders the shell should look for applications. You can easily extend it with your own directories, for example with: export PATH="$PATH:~/bin", which would add the "bin" folder in your home directory to the PATH.

The echo command / Working with text

echo is used to print text on the command line or to pipe it to another command.

Let's start with an simple example on quoting.

$ echo "Hello World"
Hello World
$ echo 'Hello World'
Hello World
$ echo Hello World
Hello World

So looks like it doesn't make a difference wether you use single, double or no quotes, does it? Well wrong, all three are actually different.

Let's go with another example:

$ echo "Hello $USER"
Hello bytee
$ echo 'Hello $USER'
Hello $USER

As you can see variables, are replaced in double quotes, but not in single ones. If you want to use variables you should go with double quotes.

Using echo without any quotes at all is also different. It's giving echo a second string to print, which could also result in a different outcome. It behaves like calling rm with multiple files / folders. (rm file1 file2 file3)

Also note that echo adds a new line after the text, to prevent that use echo -n.

Escaping

As you noticed single or double quotes are not printed, but sometimes you could need them in the output, for example when you query a database or similar.

For that you have two solutions, the easier ones is just using the opposite quotes, like echo '<a href="https://bytee.net">bytee.net</a>', but as seen above, you wouldn't be able to use any environment variables in the single quotes.

The second solution, is using the back slash () to tell the interpretor to not take the quote as command character, but just as text. This method is called escaping:

$ echo "<a href=\"https://bytee.net\">bytee.net</a>"
<a href="https://bytee.net">bytee.net</a>

As you can see the backslash are not printed, but our double quotes are. The backslash not only escapes quotes, but many other characters, like the dollar sign, spaces and itself.

$ echo "Hallo \$USER"
Hallo $USER
$ cd directory\ with\ spaces
$ echo "\\10.42.42.42"
\10.42.42.42

$ echo "\\\\10.0.42.42" # You need four backslashs to print two
\\10.0.42.42

There are also some command characters which start with the backslash (as in most other programming languages). In order for them to get evaluated you need to run echo with -e.

$ echo -e "T1:\t\tSome Text\nT42:\t\tOther Text"
T1:      Some Text
T42:    Other Text

\t is interpreted as tabulator character and \n is the new line one.