Learn Computing from the Experts | The Rheinwerk Computing Blog

Basic Rules for Bash Scripts

Written by Rheinwerk Computing | Aug 14, 2024 1:16:09 PM

Bash scripts must start with the line #!/bin/bash.

 

This line, the so-called shebang or hashbang, specifies by which interpreter the following code is executed—for example, by bash.

 

Shell scripts can only be executed if the access bits for read access (r) and execution (x) are set. The r bit is set by default for new files anyway, but you have to take care of the x bit (for execute) yourself once:

 

user$ chmod +x myscript

 

If you write a collection of your own shell script programs for daily use, it makes sense to store them in a central location.

 

The best directory to use is ~/bin. If you then make the following change in .profile, these script programs can be run without a full path specification (in some distributions this is not necessary at all, because there ~/bin is always part of PATH):

 

# addition in ~/.profile resp. in ~/.bashrc

PATH=$PATH':~/bin'

 

exit terminates the running script, and thus the script returns the return value of the last executed command. If you don’t want this, you can explicitly pass a value to exit. The value 0 signals an error-free end to the program, 1 indicates a general error, and 2 indicates an error in the passed parameters. All other error codes are application-specific.

 

In most programming languages, the following holds true: as soon as an unhandled error occurs, the program terminates. Things are much more relaxed in bash scripts: the fact that errors occur during command execution—that is, that a command ends with an error code not equal to 0—is accepted as an everyday occurrence. The execution of the script simply continues with the next statement.

 

Obvious syntax errors represent an exception—for example, for a for loop without done or for a character string that begins with " but where the second " character is missing. In such cases, bash can no longer decode the remaining program structure: it displays an error message and aborts the code execution.

 

Even though the high error tolerance of bash is often convenient, sometimes you do want a script to abort at the first error and not produce possible subsequent errors. You can achieve this by including set -e in the code. From this position on, errors lead to the immediate end. If you want this strict error control to apply to the entire script, you need to specify the -e option right in the first line:

 

#!/bin/bash -e

# this script will abort at the first error

...

 

For linked commands, set -e or the bash option -e apply only to the overall result. If cmd1 fails in the following example—even if it is because the command does not exist—then cmd2 will be executed. Only if this command also leads to an error will the script abort:

 

#!/bin/bash -e

cmd1 || cmd2

 

Editor’s note: This post has been adapted from a section of the book Linux: The Comprehensive Guide by Michael Kofler.