8.3 Create Shell Script

Now, let's actually create a shell script. This section explains a shell script creation and how to execute it.

8.3.1 Create Shell Script

Shell script is written in text file. Please use text editors, such as vi.

Let's write the shell script which executes the ls and date commands to a file called lsdate.sh.


$ vi lsdate.sh ↵       (Create lsdate.sh)

Please create lsdate.sh file which has following contents, and save it.

1 #!/bin/bash       (Shell to be Used is Specified)
2                   (Space Line)
3 ls
4 date
                  (The leftist are line numbers)

We wrote "#!/bin/bash" in first line. This describes the kind of shell to be used, and where the shell exists.

The bash is used this time. There are some shells in Linux. It specifies which shell should be used. It also specifies where the shell exists, by using command path. The shell scripts in the third line or subsequent ones will be executed by shell specified in the first line. Please be sure to make the 2nd line into space.

Next, let's change the permission of the lsdate.sh file, to make it executable. Please check the execution authority of lsdate.sh.


$ ls -l lsdate.sh -rwxrwxr-x 1 okada okada 21 Jun 6 09:51 lsdate.sh

Check whether the file is executable or not. If not, execute following command.


$ chmod u+x lsdate.sh ↵

This made the file executable to owner. Let's execute it.


$ ./lsdate.sh ↵
Desktop       diff2             ls-l-output                ls-usr-bin    touched-file
cat-output    hosts.bak    ls-l-output-second    lsdate.sh    uniq-sample
diff1          hosts.new2    ls-output score       (execution result of ls)
Fri Jun 6 09:55:05 JST 2008                      (execution result of date)

"./" is path description. It means current directory. "./lsdate.sh" is instruction which executes lsdate.sh file under current directory. ls and cp don't need this kind of instruction, because their path is usually already set. In order to perform the specific shell script in current directory this time, it used as path specification.

We can check that both ls command and date command, which are written in lsdate.sh, were executed.

8.3.2 Variable

Variables is one of the important factors of programming. For example, the variables are x or y in mathematics of junior high school. In shell script programming, we can set numerical values or character strings into variables, and use them.

We can set any values into variable, by using "=".

Practice: Create shell variables

$ abc=123 ↵
$ echo $abc ↵ bsp;          (Display the value of abc)

We set 123 into variable abc.

We can use one-dimensional array in bash. Each element is surrounded by square brackets []. When displaying the contents of the array variables, the array variables are surrounded by wave brackets {} behind $.

$ abc[0]=123 ↵
$ abc[1]=456 ↵
$ echo ${abc[0]} ↵           (Display the value of abc[0])
$ index=1 ↵
$ echo ${abc[$index]} ↵      (Display the value of abc[1] by using variable)

There are two kinds of variables in shell script programming. They are shell variable and environment variable. Shell variables are effective only inside the shell script which is being currently performed. Environment variables are effective even inside the command executed from the shell script.

The environment variable can be created from shell variables.

Practice: Create environment variables

$ export abc ↵           (Make from shell variable to environment variable)
$ export xyz=234 ↵      (Make an environment variable xyzmore, and set 234 into it)

The first line made an environment variable abc from shell variable. The second line made an environment variable xyz, and set 234 into it.

Next, let's look at the difference between shell variable and environment variable. There are two shell script BBB.sh and CCC.sh. The contents of BBB.sh are following.


$ cat BBB.sh ↵


export xyz=234
(When BBB.sh executes CCC.sh, CCC.sh can not get the value of abc but can get the value of xyz.)

When this script runs, CCC.sh is unable to know the value of abc, but know the value of xyz is 234. Because abc is shell variable, and xyz is environment variable.

8.3.3 echo Command

The echo command outputs character string to standard output. echo can also output variables by attaching '$' before them.


echo [options] string


-n Control new line character. Although the echo command usually outputs new line character at the end, if this option is specified it doesn't.

Practice: Display shell variable

$ abc=123 ↵
$ echo abc ↵
$ echo $abc ↵
123       Displayed the value of variable

8.3.4 read Command

The read command reads data from standard input.


read variable

Practice: Execute read command

$ echo $abc ↵
123                   The value of the shell variable abc is displayed.
$ read abc ↵
aaabbbccc       Input something
$ echo $abc ↵
aaabbbccc       The value of the shell variable abc was changed.

8.3.5 List Variables and Delete them

The set command is used when displaying the list of shell variables. The unset command is used when deleting variables.

Practice: Display shell variable list and delete an variable

$ set ↵
      :       The list of variables, This includes environment variables.
$ unset abc ↵             Delete shell variable abc

The env command is used when displaying the list of environment variables. If you use export command with no value, the environment variable is deleted.

Practice: Display environment variable list and delete an variable

$ env ↵
      :       The list of variables environment variables
$ export abc=             Delete environment variable abc

8.3.6 Option of Shell Script

When writing shell scripts, there are some required knowledges.

Comments are the notes written on programs. Although it does not have any meanings at all on a program, those who write programs can share important information about them. Comments are very effective descriptions.

The line which starts with '#' is comment line, it is disregarded in program.



# insert xyz into value abc and output it
echo $abc

A line '# insert ....' is disregarded, so there is not any impact at all.

This section explains quotation-marks. This includes single quote"'", double quote""", and back quart"`".

The difference between a single quote and a double quote is how to handle variables. In case of double quote, the value of variable is used. The commands which are surrounded by back quarts, are executed and their results will be used.

Practice: Check how quotation marks work

$ abc=xyz
$ echo 'value abc is $abc'
value abc is $abc       $abc has been handled as character string.
$ echo "value abc is $abc"
value abc is xyz       $abc has been handled as a variable, so its value xyz was used.
$ abc=`date`;
$ echo $abc
Thu Mar 20 06:08:14 JST 2008     $abc has the execution result of the date command.

Shell script can use arguments as options. We can handle the arguments in $numeric format.

Practice: Check the value of arguments

Let's make following file and execute.

$ cat args.sh ↵
echo '$1:' $1;
echo '$2:' $2;
echo '$3:' $3;
echo '$0:' $0;
echo '$#:' $#;
$ ./args.sh aaa bbb ccc ↵
$1: aaa             The first argument is $1
$2: bbb             The second argument is $2
$3: ccc             The third argument is $3
$0: ./args.sh       $0 has executed command name
$#: 3                   $# has the number of arguments

The shift command shifts an order of the arguments. If shift command is executed, $2 becomes $1 and $3 becomes $2 ...

Practice: Check how the shift command works

echo '$1:' $1;
echo '$2:' $2;
echo '$3:' $3;
echo '$1:' $1;
echo '$2:' $2;
$ ./argsshift.sh aaa bbb ccc ↵
$1: aaa
$2: bbb
$3: ccc
$1: bbb             $1 changed to bbb
$2: ccc             $2 changed to ccc

There are some special characters in programming. For example, let's consider the case where the echo command is used. When you want to output double quote '"', you may enter following command string.

echo """ ↵             The double quote was surrounded by the double quotes.

However this is judged wrong instruction, because three double quotes can't be pairs. It is necessary to tell "The second double quote is a character which should be output, not one of the quotation marks."

In such a case, we can use escape sequence. Shell scripts use character '\' as escape sequence.

Practice: Check how escape sequence works

$echo "\"" ↵
"             Outputted double quote

When you make shell script, you may want to use other shell script. In such a case, you can use source command.


source /etc/init.d/functions
. /etc/init.d/functions

This reads /etc/init.d/functions file. You can use character '.' for same function, instead of source command.

When character strings become long, you can continue its sentence in next line, by putting \ (backslash) at the end of the line.

$ echo "abcedfghijklmnopqrstuvwxyz" ↵
$ echo "abcedfghijklmn\ ↵
> pqrstuvwxyz" ↵
abcedfghijklmnopqrstuvwxyz             We can get same result, even if there is a backslash on the way.

The new-line by a backslash can be used also within shell scripts.

Previous Next