Bash

Shell

A shell acts as an interface between you and the UNIX kernel (to be referred to as Linux from now on). It is pretty much like the good old command line DOS, but much more powerful, robust and versatile. Its a kind of high level programming language in its own right. Often abbreviated as sh (the original shell shell, written by Steve Bourne), Linux comes with the bash shell, better known sometimes as the Bourne Again shell. Click here for some history.

If you have a GNU/Linux box, you already have a working bash shell. To figure out which version you have, just type

/bin/bash -version

on my system, the output of this command is

GNU bash, version 3.2.9(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.

You can also use

echo $BASH_VERSION

which in my case shows this

3.2.9(1)-release

You can also use the shell as /bin/sh, and on most systems this will be a symbolic link to bash:

lrwxrwxrwx 1 root root 4 2007-07-14 18:39 sh -> bash

A shell program

# first.sh
# This is a comment. But the next line isn't.

#!/bin/bash

echo HELLO

Shell programs can be run two ways: interactively or as programs. In the interactive mode, you start the shell and type commands and they are executed as you type them. If you write a bunch of commands in a file (as we just did, for first.sh) and execute them later, you use make a program, or to be precise, code which can be executed by invoking the shell after it has been put together.

echo simply takes the argument and dumps it on the standard output. If you have environment variables like TERM or HTTP_PROXY or BASH_VERSION, yuo can print them too, by putting a $ sign before their names, as we did to print the version number of bash. # is for comments, but #! tells the shell that the argument on the right of ! is to be used to execute the shell script. /bin/bash can be replaced by /bin/sh as sh links to bash anyway.

The script can be run in two ways:

sh first.sh

or

chmod +x first.sh
./first.sh

Note that shell scripts do not have to have extensions of the form .sh. first is perfectly valid as a filename too. The second method makes the script executable (this is okay because of #!/bin/bash so the "program" really is self contained and doesn't need an extra bash or sh to execute it from the command line).

Also, if you run it as

first.sh

it might not work as the environment variable PATH isn't set to look for stuff in the working directory (where your program is stored). If you're running this code as root, then don't change the PATH variable (this is a potential security loophole, as a superuser can execute fake copies of standard commands). Its always better to pre-pend ./ to all local commands.

All the code you type into a shell script can just as well be executed on the command line. You can use this to verify something without having to write code for it. Suppose you defined a variable x and set it equal to 4. If you want to know what it is, you can do this from the command line itself:

$ x = 4
$ echo x
x
$ echo $x
4

This is not surprising, because the command line is, after all, the shell itself :-). To query an environment variable, you need to prefix $ to its variable name.

Strings should be delimited by inverted commas " and ":

echo "bash is so cool"

and to actually print $, you need to use \$:

$ echo \$x = $x
$x = 4

Related:

  1. How to set Shell Environment Variables (bash shell)
  2. Linux Bash commands - MAN pages

If you want help on any bash command, type

man bash-command-name

We can also used read to take basic input:

#!/bin/sh

echo "Enter something: "
read something
echo "You entered "$something

conditions

The test command can be used, among other things, to determine whether a file exists in the working directory or not.

#!/usr/bin/bash

echo "Enter a filename: "
read filename

if test -f $filename
then 
   echo $filename" exists"
else 
   echo $filename" does not exist"
fi

The if-then-else construct has this form in bash:

if <condition>
then
    <statement block 1>
else
    <statement block 2>
fi

Note that fi marks the end of the construct (its role is equivalent to the role of parentheses { and }). Instead of the test command, we can use

[ -f $filename ]

with a space between [ and ]. Here the argument between [ and ] is an argument to test. That is "test -f first.sh" is equivalent to [ -f test.sh ].

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License