Shell Variables

As a shell script is essentially just a string of shell commands, it understands the current environment variables. For example:

echo "Your current directory is" $PWD

Here $USER and $PWD are the environment variables that are already accessible to the shell representing the current user and the working directory, respectively.

If you save this in a file, you have a simple shell script. The first line tells the system that it is to interpret the script using /bin/bash. This line applies if you make the script executable and run it directly, and it is important because it is possible that the user might be running a different shell. In fact, you can run the script in various ways:

■ Under the current shell by sourcing it:

[email protected]:~> source Hello fred

Your current directory is /home/fred

[email protected]:~> . Hello fred

Your current directory is /home/fred

■ You can also run the script by explicitly calling bash:

[email protected]:~> bash

■ Or, because /bin/sh is a symbolic link to / bin/bash, you can do the following:

[email protected]:~> sh

Alternatively, you can make the script executable and run it as a program. This is done by the chmod command, which adds executable permissions for all. Note that if you want to run the script in this way, it must have the first line:

This tells the system that the interpreter needed to run the script is bash. If you source the script, this is not necessary:

[email protected]:~> chmod a+x [email protected]:~> ./ Hello fred

Your current directory is /home/fred

A shell script can include the types of logical structures that you would expect to find in any programming language. For example, a for loop:

do echo $i done

Here, $i represents the variable i , which takes the successive values in the loop.

Save this as, make it executable, and run it as you have previously, and you'll see the following:

[email protected]:~> chmod a+x [email protected]:~> ./ l

Although you are unlikely to use numerical variables often except as basic counters in keeping track of a repeating process, you can do arithmetic in shell scripts:

echo "a is "$a echo "a+1 is " $((a+1))

This script produces output like this:

Notice that there should be no white space around the = sign; if there is, the variable a will not be assigned. Notice also the way that extra brackets were needed in

Variables can equally represent strings:


Here, to get the desired result, you had to write ${a}FGHIJ rather than $aFGHIJ, which would have represented a new and as yet undefined variable (named aFGHIJ). This ensures that bash interprets the last line as a request to echo the variable, which we defined as a, followed immediately by the string FGHIJ.

Suppose you want to produce word count data for all the files with the file extension .txt in the current directory.

This produces output as shown in the following lines, indicating that you can loop over a set of files produced by globbing — the use of a notation such as *.txt.

Clearly you could take this idea further to select, for example, certain lines from each of a set of files and append them to a file:

#! /bin/bash for i in *.html do grep -i \<title\> $i >>titles.list done

This creates a file containing all lines of the form:

<title>Page Title</title>

from any of the HTML files in the current directory. Or you could do anything else that can be done with one or more commands acting on each file and direct the output to another.

Certain variables have built-in meanings. In particular, some variables represent the arguments that are given to the script when you run it (sometimes known as positional parameters). So, for example, consider a script with these contents:

#! /bin/bash echo "the first argument is" $1 echo "the second argument is" $2

If you just run ./, you will see no output because the command line contains no arguments. But if you do the following:

[email protected]:~ > ./ this that the first argument is this the second argument is that you get the output because you have provided a first argument, this, and a second argument, that.

Suppose you want to take the first two lines of one file and append them to the end of another file:

Save this as Now, suppose you have files a and b with these contents:

To use the shell script on files a and b, you run the following:

When the shell script is done, you can examine the results:

a line 4 a line 5

[email protected]:~ > cat b b line 1 b line 2 b line 3 b line 4 b line 5 a line 1 a line 2

You can see that the shell script has taken a line 1 and a line 2 and appended them to the end of file b.

This kind of operation has great simplicity and power at the same time if used with a little ingenuity. For example, many people do something like this:

Here, the $1 represents the first argument to the script, and the -l is the login username being passed to the ssh program.

Save this output as a file with the name root in ~ /bin (the directory bin under your home directory, which should be in your path). Then you can simply type:

[email protected]:~ > root remote

This makes the script run the command ssh remote -l root, so you will be logging in as root to the machine remote. This example, although very simple, illustrates the value of being able to use arguments with a script, something you cannot do with an alias. Of course, for this very simple use, you could define an alias like the following:

alias root='ssh -l root'

In general, however, a script can take as many arguments as you want. For example, you could modify the previous script to take both the user and the machine as arguments simply by making the second line ssh $1 -l $2. This functionality could not be achieved with an alias.

Was this article helpful?

0 0

Post a comment