Introduction to Bash

Overview

bash, the Bourne-Again Shell, refers both to a particular Unix shell program and its associated scripting language. It is the default shell of the GNU Operating System (Linux) and is POSIX 1003.2 compliant. It is a powerful shell, and features

Links

bash, the Shell

What's a Shell?

A shell is a command interpreter. Commands can be executable files or built-ins. Commands can be bundled together into a script which a shell program executes. How the commands are packaged and wired together, using variables, functions, and control-flow operators makes up the shell's scripting language.

Examples of Shells

Organized by family:

Also see Christopher Browne's Unix shell page and Wikipedia's Unix shell page.

Using Bash

Like all shells, bash can be run interactively or non-interactively. An example interactive session:

ray@siouxsie:~$ x=4
ray@siouxsie:~$ echo x
x
ray@siouxsie:~$ echo $x
4
ray@siouxsie:~$ echo "hello"
hello
ray@siouxsie:~$ echo $PS1
\u@\h:\w\$
ray@siouxsie:~$ PS1="\w\$ "
~$ mkdir test
~$ cd test
~/test$ ls
~/test$ cat > message
here is some
text
~/test$ cat message
here is some
text
~/test$ ls
message
~/test$ ls -l
total 1
-rw-rw---- 1 ray ray 18 Nov 12 22:42 message
~/test$ ls -la
total 9
drwxrwx---  2 ray ray 2048 Nov 12 22:42 .
drwxr-xr-x 52 ray ray 6144 Nov 12 22:42 ..
-rw-rw----  1 ray ray   18 Nov 12 22:42 message
~/test$ cp message new
~/test$ ls
message  new
~/test$ du
4       .
~/test$ df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/hda6              5044156   3518624   1269300  74% /
varrun                  517076        88    516988   1% /var/run
varlock                 517076         0    517076   0% /var/lock
udev                    517076       116    516960   1% /dev
devshm                  517076         0    517076   0% /dev/shm
/dev/hda1              1011928        20    960504   1% /rescue
AFS                    9000000         0   9000000   0% /afs
~/test$ rm message
~/test$ help kill
kill: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
     Send the processes named by PID (or JOBSPEC) the signal SIGSPEC.  If
    SIGSPEC is not present, then SIGTERM is assumed.  An argument of `-l'
    lists the signal names; if arguments follow `-l' they are assumed to
    be signal numbers for which names should be listed.  Kill is a shell
    builtin for two reasons: it allows job IDs to be used instead of
    process IDs, and, if you have reached the limit on processes that
    you can create, you don't have to start a process to kill another one.
~/test$ y = 5
-bash: y: command not found
~/test$ y=5
~/test$ echo x+y
x+y
~/test$ echo $((x+y))
9
~/test$ jobs
~/test$ printf '%d + %d = %8d\n' 9 4 13
9 + 4 =       13
~/test$ seq 4 10 3
~/test$ seq 1 5
1
2
3
4
5
~/test$ for w in $(seq 1 5); do echo $w; done
1
2
3
4
5
~/test$ cat > stuff
first line
second line
third line
fourth line
fourth line
~/test$
~/test$ sort < stuff
first line
fourth line
fourth line
second line
third line
~/test$ grep line stuff
first line
second line
third line
fourth line
fourth line
~/test$ grep line stuff | sort | uniq
first line
fourth line
second line
third line
~/test$

And here are some example scripts:

# The classic hello world script.

echo Hello World
# A script to print some Pythagorean triples.

for c in `seq 1 100`; do
    for b in `seq 1 $c`; do
        for a in `seq 1 $b`; do
            if [ $(($a * $a + $b * $b)) == $(($c * $c)) ]; then
                echo $a $b $c
            fi
        done
    done
done
# A script to write the Fibonacci numbers up to and including the
# first commandline argument.

a=0
b=1
while [ $b -lt $1 ]; do
    echo "$b "
    olda=$a
    a=$b
    b=$(($olda + $b))
done

Good to know

Make these snippets of knowledge second nature:

Built-in commands

Remember a command is either an executable file or is built into the shell. These are the builtins:

.
:
[
alias
bg
bind
break
builtin
caller
cd
command
compgen
complete
continue
declare
dirs
disown
echo
enable
eval
exec
exit
export
fc
fg
getopts
hash
help
history
jobs
kill
let
local
logout
pushd
popd
pwd
printf
read
readonly
return
set
shift
shopt
source
suspend
test
times
trap
type
typeset
ulimit
umask
unalias
unset
wait

Special Files

Exercise: Many people's ~/.bash_profile contains only the line
      if [ -f ~/.bashrc ]; then . ~/.bashrc; fi.
Why?

bash, the Language

Reserved Words

    !         time
    [[        ]]       {       }
    if        then     elif    else    fi
    case      esac
    select    in
    while     until    for     do      done
    function

Command Syntax and Execution

TODO

More

These notes are only a brief introduction, so read the Bash Reference Manual. Some things not covered here include: