shell programming - intro

created onApril 18, 2022

Shell programming can be used to write shell scripts that simplify tasks that can be done on the command line. Shell scripts consist of a consecutive series of terminal commands and shell built-in commands. This documentation refers to bash, the bourne again shell.

shell script applications

  • Unix / Linux system configurations heavily rely on shell scripts. In oder to analyze or modify the system configuration, you need shell programming skills.
  • startup scripts for servers, i.e. name servers, DBMS servers, webservers.
  • simple processing of text and log files.
  • simplified startup of user programs.

when not to use shell scripts

the bash has its limitations and shell scripts are not applicable for complex tasks.

a shell script is no option in the follwoing scenarios:

  • you need more complex data structures like multi-dimensional arrays, linked lists and hash maps. In bash, you are stuck with variables and one-dimensional arrays.
  • complex applications that require structured programming with techniques like variable type checking, function prototypes, complex data types like structs, OOP.
  • resource intensive operations like sorting or hashing on huge amounts of data.
  • extensive file I/O. bash has limited file I/O capabilities: file access is only serial, file read access is only line by line.
  • mission critical applications.

My personal limit for a good shell script is 100 lines (without any included texts for the online help). I occasionally find myself breaking this rule.

the shebang

Shell scripts usually start with what is called a shebang, like i.e.:

#!/bin/bash

the in the line above is the shebang. It is followed by the absolute path to the executable of the shell. The line defines that the script is to be executed in a bash shell. Here’s a simplicistic shell script that just puts out hi there, followed by a newline, on the terminal and then exits.

#!/bin/bash printf ''%s\n' "hi there"

The script above can also be written without the shebang line:

printf '%s\n' "hi there"

in case there is no shell specified by the shebang, the script is executed in the login shell, which can be determined by checking the line for the username in :

$ grep rudolf /etc/passwd rudolf:x:1000:1000:rudolf,,,:/home/rudolf:/bin/bash $

You can also specify in which shell the script is to be executed, i.e. a script:

#!/bin/bash printf "%s\n" "hi there"

could be run in the c-shell:

$ csh intro-0.sh hi there $

In this case, the shell specified by the shebang line is ignored and the script is run by the shell specified on the command line (in this case, the c-shell). This works because in great portions of their syntax, the syntax of the different shells is identical. great portions also means that there are differences in the syntax. Also the behaviour of the different shells in certain situations is different. This is why it’s not such a good idea to overwrite the shebang line and run a, say bash script, in a c-shell, except for trivial scripts.

In the shebang line, the exclamation mark has to follow the hash symbol immediately, otherwise the line is interpreted as a comment. There are systems that allow blanks between and the absolute path of the shell in which the script is to be executed, but if you want to play it safe, do not use whitespaces here either.

making shell scripts executable

In order to make a shell script executable, the executable bit of the shell script must be set, i.e. with or .

If the shell with which to run a script is explicitly specified when running the script, the executable bit doesn’t have to be set. I.e.

$ /bin/bash somescript.sh

runs , regardless whether the executable bit has been set or has not been set.

comments

A comment starts with . Comments can start anywhere on a line. Everything after the up to the end of the line is considered as part of the comment.

The shebang has to be on the first line of the script, otherwise it is treated like a comment, which starts with the hash symbol :

#!/bin/bash # some comment for demonstration printf "%s\n" "hi there" # ... and another comment

terminating scripts and exit codes

A shell script terminates after the last command in the script or when terminated with the command . Every process in Linux returns an exit code (also referred to as exit status) on termination and so do shell scripts. In a shell script, the default exit code is the exit code of the last command of the shell script, but the exit code can be be set explicitly with .

The common standard for exit codes is that upon successful completion, a process returns an exit code of 0, and in case of an error an exit code from the range of 1 to 255, although there are exceptions.

there are some reserved exit codes, that shouldn’t be used when setting the exit code explicitly in a shell script:

exit meaning comment
1 catchall for errors general errors like division by zero
2 misuse of shell builtins according to bash manual
126 command invoked can not be executed command is not executable or permission problems
127 command invoked can not be found command does not exist or typo in path to command
128 invalid argument to exit invalid argument type like floating point numbers or strings given as argument to exit
128 to 192 shell script stopped by signal n - 128, where n is the exit code i.e. 130 for CTRL-C, run kill -l in terminal for the list of signals
255 exit code out of range range of valid values for exit code is 0 -255