image: shell script
Making a shell script that uses environment variables executable
R E L A T E D   C O N T E N T
ADVERTISEMENT

Hands on: Command post

Advanced features of the shell explained

Barry Shilliday, Personal Computer World 08 Oct 2007
ADVERTISEMENT

Last time we looked at bash, the standard Unix shell found on Linux and OSX systems.

Although it is often seen as old-fashioned, the shell can make certain tasks easier and faster to perform than loading up a GUI application to do the same, and it is definitely worth getting to know.

This time we will look a little more at the shell, covering a few advanced features.

Environment variables
A shell is essentially an interpreter for running commands. It can operate interactively at a prompt or run through a programmed sequence of commands, known as a shell script. In both states the shell maintains a unique environment. If you run a separate shell in another terminal it will have its own environment.

In programming languages such as Basic and C you will have heard of variables ­ names that can hold a value. Bash has the same, but known as environment variables.

An environment variable is identified by a straightforward name, such as USER or MYVAR. It is not necessary for the identifier to be in capitals, but this is the universal standard, as it makes clear you are talking about a variable and not a command name. When you want to use the value of an environment variable, the name is prefixed with a dollar sign; thus the contents of USER is expressed with $USER.

The shell automatically sets up a few environment variables when it starts up. One of these is USER, which holds the name of the user that ran the shell. Another is SHELL, which contains the name of the shell. HOME is set to the home directory of the current user. Each of these can be shown by running ‘echo’. This usually prints something to the terminal. For example, ‘echo hello’ will print ‘hello’ on the terminal. Printing an environment variable is just as easy:

$ echo $USER
barry
$ echo $HOME
/home/barry

What happens here is that the shell substitutes whatever is held in the environment variable and sends the result to the echo command, as if you had typed it yourself. The distinction is important ­ the echo command does not have to process the environment variable itself.

These variables are commonly used in shell scripts. A script could be run by any user, so the script could, for example, check the contents of USER to determine if it is being run as root or not.

The PATH variable
When a bash shell starts up it runs through a few scripts automatically. One of these is /etc/profile, a system-wide script for setting up the shell environment. The PATH variable is typically set here, either directly or indirectly. This is one of the most important environment variables, as it determines how commands can be run.

As we saw last month, if you run ‘ls -l’ in the shell, the interpreter finds the ‘ls’ command on the filesystem and executes it with the argument ‘-l’. It does not, of course, have to guess where to find the command. Instead it looks at the PATH variable, and searches each directory in turn for an executable command called ls. It will execute the first one it finds, or if after searching every directory, if can’t find ‘ls’, it will return a ‘command not found’ error. Have a look at the PATH variable with echo:

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

This is the standard path for Ubuntu Linux. When typing a command, such as ls, the shell first checks /usr/local/sbin, and proceeds through the rest, with each directory separated by a colon.


All PC Operating Systems
Tags: Windows

Like this story? Spread the news by clicking below:

Post this to Delicious del.icio.us    Post this to Digg Digg this    Post this to reddit reddit!

Permalink for this story
R E A D E R   C O M M E N T S
M A R K E T P L A C E
Get your free demo of Numara Track-It! 8 - the leading help desk solution for IT related issues.
Make presentations, review documents & share your entire desktop. 30-day free trial! (cc required).
Discover how remote support can fuel your IT business in ways you've never thought of before.
Apply ITIL best practices at your service desk while eliminating integration cost. Learn more here.
WAN based, automated, daily vulnerability assessments. Click here to try and request our whitepapers.
Have your product or service listed here >   
Sponsored links
F E A T U R E D   J O B S
London, United Kingdom | City of London
ICT Support Officer £27,320 - £33,370 pa inc. depending on experience (pay award pending) Maternity cover for up to one year Guildhall, London EC2 Bring your IT experience to one of the country's most prestigious ... more >
London, Haringey, United Kingdom | Haringey Council
PMO Support Officer - Haringey, London - £32,289 - £37,542 pa   Experienced project support officer required by the internal IT services organisation of a London borough council to work within its Programme Management Office ... more >
London, United Kingdom | Deloitte
Technology and Systems Consulting Event - LondonWith the right balance, you'll achieve great things. Join our Consulting practice and have the opportunity to balance your technical and business consulting skills to bring out the best ... more >
Leek Wootton, United Kingdom | Warwickshire Police
 IT Business Analyst - Leek Wootton, Warwickshire - £29,112 - £31,491 PA - 37 hrs per week   Everyone who works for Warwickshire Police helps to protect our communities from harm. Work with us and ... more >
More job opportunities