Shell scripting tutorial
This is a tutorial for bash shell scripting.
Contents
Hello world
The easiest way to get your feet wet with a programming language is to start with a program that simply outputs a trivial text, the so-called hello-world-example. Here it is for bash:
echo "hello world"
Note that you can open a console and enter this command or you can put it into an executable file, say "foo.sh" and call the file in a console like this
./foo.sh
Or you can explicitely use bash to interpret the commands in this file:
bash foo.sh
In all cases the output will be just
hello world
on your console (to be more general: on stdout).
There is also a way to tell the script that it is explicitely written for bash (and no other shell using a shebang line like this in your file:
#!/bin/bash echo "hello world"
input
To show you how to deal with variables, we will now write a script that asks for your name and greets you:
echo "what is your name? " read name echo "hello $name"
You see that the name is stored in a variable $name. Note the quotation marks " around "hello $name". By using these you say that you want variables to be replaced by their content. If you were to use apostrophes, the name would not be printed, but $name instead.
common mistakes
Note that the variable is called $name, however the correct statement to read it is
read name
It is a common mistake to write
read $name
which means "read a string and store it into the variable whose name is stored in $name"
return codes
Every bash script can communicate with the rest of the system by
The return code is 0 if everything worked well. You can query it for the most recent command using $?:
bootstick@bootstick:~$ echo "hello world"; echo $? hello world 0 bootstick@bootstick:~$ echo "hello world">/proc/cmdline; echo $? bash: /proc/cmdline: Permission denied 1
In bash, true is 0 and false is any value but 0. There exist two commands, true and false that deliver true or false, respectively:
bootstick@bootstick:~$ true; echo $? 0 bootstick@bootstick:~$ false; echo $? 1
conditions
echo "what is your name? " read name if [ $name = "Thorsten" ]; then echo "I know you"; fi
arithmetic expressions
echo "what is your age? " read age if (( $age >= 21 )); then echo "Let's talk about sex."; fi
common mistakes
Common mistakes are:
- to forget the blank behind/before the [ or ] character
- to forget the blank behind/before the equal sign
- see what does "unary operator expected" mean
line feeds
Let's look at the following script:
read name if [ $name = "Thorsten" ]; then echo "I know you"; fi
Instead of a semicolon you can write a line feed like this:
read name if [ $name = "Thorsten" ] then echo "I know you" fi
And instead of a line feed you can use a semicolon:
read name; if [ $name = "Thorsten" ]; then echo "I know you"; fi
If you want to insert a line feed where you do not need one, e.g. to make the code better readable, you must prepend it with a backslash:
read \ name if [ $name = "Thorsten" ] then \ echo "I know you" fi
calling commands
Calling commands in a bash script is as easy as it can be: You just write the command to be called, like this:
echo "Now calling a browser" firefox echo "Continuing with the script"
Redirections
filling files
To create a file, probably the easiest way is to use cat:
cat >README<<EOF This is line 1 This is line 2 This is the last line EOF