09Shell Variables
09Shell Variables
This covers shell variables and introduces user environment. We will discuss the basics of Shell programming language.
- Shell Variables
- Substitution operators
As any other language, the shell provides the user with the ability to define variables and assign values to them. A Shell
variable has a similar to that of filename. The names for variables begin with an alphanumeric character or an underscore,
followed by one or more alphanumeric or underscore characters. Some valid shell variables include the following:
HOME
DOS_SHELL
DIR1
S_PI_BY_2
Also, shell variable names are case-sensitive. So, the words extra, eXtra, EXTRA are three completely different variables
as far as the shell is concerned.
The shell does not recognize a digit as the first character of a variable.
DO and DON’T
DON’T use any other punctuation marks than the underscore
DON’T put any spaces on either side of the equal sign when assigning a value to a variable
DO realize that shell variables are also case-sensitive, just like filename.
DO use meaningful variable names, just like filenames. Names such as abcd.c do not mean much to another user, or even
yourself if you come back to look at it after some time.
DO use uppercase names for environment variables if you want to stand out in listing and to not be confused with
lowercase LINUX commands.
Uses of Shell Variables
Shell variables are very useful in a lot of ways. Two of the most common uses are as follows:
- As placeholders for pathnames
- As options to common commands
The first use shortens the typing required to traverse multiple directory paths. For Example:
$ pwd
/home/mdhlinux
$ tempdir=/home/mdhlinux/shellprog/basics
$ echo $tempdir
$ cd $tempdir
$ pwd
/home/mdhlinux/shellprog/basics
To assign the directory name to a variable, we can use the following command.
$ tempdir=/home/mdhlinux/shellprog/basics
We can switch over to the directory by specifying the value held by the variable tempdir. The shell substituted the value
of tempdir for the part of $tempdir before passing the argument to the cd command.
The shell does variable substitution before the echo command is executed. All variables preceded by a $ character are
replaced with the shell before being passed to echo command. Both count and tempdir are evaluated and the values
resulting from these applications are passed to the echo command.
Shell Variable Considerations
The shell has no concept of numeric values. All values are strings as far as it is concerned. The values of 1, one, and 1.23
are all stored in variables as strings.
Unlike variables in higher programming languages such as C or Pascal, you do not have to declare a shell variable before
you use it. If you use a shell variable without first setting it, the shell will declare it for us and proceed with an empty
string value. So, when in doubt about the value of a shell variable, use the echo command to display the content of the
variable.
It’s up to you to confirm that the variable you are using has a value in it. Keep in mind that the shell does this substitution
before it passes the arguments to the command. So what happens when you try to echo a variable that does not have a
value?
$ echo $unknow
$_
To see if the value is indeed blank, put colons around it to get the following:
$ echo :$unknown:
::
$_
This variable now contains a NULL value. Shell variables with a NULL value are completely removed from the command
line without a trace. So a command like the following:
For example, you do have to type ls -al every time you want long listing. Instead of typing this sequence all the time and
subjecting yourself to typos, use a substitute for that command with an environment variable:
$ command=”-al”
$ ls $command
Above is same as ls -al command.
Note that this is a taste of what shell scripts are about. Shell Scripts provide many more features, such as arguments and
consistency, and they are much more reliable. Plus, you do not have to type the $ before the command name to invoke
them.
[mdhlinux@localhost ~]$ VOOT=/
[mdhlinux@localhost ~]$ NOTHING=bad
Resetting a Variable
When a variable has no value, it defaults to NULL. You can think of NULL as an empty string. To set the value of a shell
variable to NULL, you can choose from at least three different ways
$ myvalue=
$ echo $myvalue
$ myvalue=#
$ echo $myvalue
$ myvalue=””
$ echo $myvalue
The first assignment is terminated with a return immediately following the equal sign.
The second assignment is terminated with a comment character.
The third assignment is set to double quotes with no space between them.
Using Shell Variables
Your HOME shell variable is set to the directory into which you are placed when you log in. So, wherever your current
working directory may be, you can go to your login directory by typing the following:
$ cd $HOME
In this case of cd, if no pathname is specified, the value of $HOME is implied as the input directory name.
You can copy the file at your current location to you home directory:
$ cp file $HOME
This would copy the file in your current directory to your HOME directory, regardless of where you are in the directory
tree.
[mdhlinux@localhost ~]$ value1=88
[mdhlinux@localhost ~]$ value2=value1
[mdhlinux@localhost ~]$ echo $value2
value1
[mdhlinux@localhost gamma]$ ls
[mdhlinux@localhost gamma]$
[mdhlinux@localhost ~]$ cp $d/*.cpp $s
[mdhlinux@localhost ~]$ cd $s
[mdhlinux@localhost gamma]$ ls
test.cpp
As you can see, by setting the variable s and d once, you ease the burden of typing long pathnames. It’s a lot easier to
type $s than to type /home/mdhlinux/lab/alpha/beta/gamma, and you are less likely to make mistakes.
Constructing New Variables
Frequently, you will want to append some letters to filenames. For example, to copy oldFlame to oldFlame.old, shown in
the variable BUGSYS, you might be tempted to use the following command:
$ mv $BUGSYS $BUGSYS.old
You must ensure that BUGSYS is set to oldFlame. This command translates to the value of BUGSYS into the first argument.
The shell then attempts to translate the second variable to the value of BUGSYS.old. If that variable does not exist, you
get an error. Sometimes this may not work.
The curly braces { } remove the ambiguity. This command works because the shell first replaces the value within the
curly braces before it appends the old string to it.
$ FILE=test
$ mv $FILE ${FILE}.c
This feature of the shell enables you to create new variables from another. You could get a set of files with different
extensions from the same base name.
[mdhlinux@localhost lab]$ bname="Chapter"
[mdhlinux@localhost lab]$ chap1=${bname}.1
[mdhlinux@localhost lab]$ chap2=${bname}.2
[mdhlinux@localhost lab]$ chap3=${bname}.3
[mdhlinux@localhost lab]$ echo $chap1
Chapter.1
[mdhlinux@localhost lab]$ echo $chap2
Chapter.2
[mdhlinux@localhost lab]$ echo $chap3
Chapter.3
Three files are created from the base name. These names can now be used to cat, rename, copy, move, or otherwise
manipulate files.
Local Versus Environment Variables
The environment variables refer to those shell variables that LINUX sets for you when you log in and the ones you set for
shortening your commands.
Local Variables in the shell are variables that are local to your shell, which no other standard LINUX process needs to
know about. Local Variables could be variables that exist for a short time during a shell script. They are local to your shell
environment. For example, you could set a variable called count to whatever you desire, without it affecting the way your
standard commands work.
Your shell environment contains a set of variables that identify specific files, paths, and other information for you. You
cannot override all system defaults, but it’s probably not a good idea to change some of these unless you know what you
want. For example, changing the value of the HOME directory changes the reference to your login directory. Some
variables such as HOME are default parameters for LINUX programs.
[mdhlinux@localhost gamma]$ pwd
/home/mdhlinux/lab/alpha/beta/gamma
[mdhlinux@localhost gamma]$ echo $HOME
/home/mdhlinux
[mdhlinux@localhost gamma]$ cd
[mdhlinux@localhost ~]$ pwd
/home/mdhlinux
[mdhlinux@localhost ~]$ cd lab/alpha/beta/gamma
[mdhlinux@localhost gamma]$ cd ~
[mdhlinux@localhost ~]$ pwd
/home/mdhlinux
The cd command returns you back to your home directory when no argument was specified to the cd command.
The HOME variable is used as the default directory of where you login. It’s not a good idea to change the value in this
variable, since a lot of standard LINUX script files might not work.
Some programs require certain environment shell variables to be set to a certain value. For example, a package for an
application may require that the library files for its programs be specified in the LIBPATH or a combination of the HOME
environment variable. If this variable is not set correctly, your application might not run correctly. Before you change an
environment variable’s value, check its impact first.
$ set
BASH=/bin/bash
BASH_VERSION='4.2.46(1)-release'
COLUMNS=143
HISTFILE=/home/mdhlinux/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HOME=/home/mdhlinux
HOSTNAME=localhost.localdomain
HOSTTYPE=x86_64
ID=1023
LANG=en_US.UTF-8
LINES=38
LOGNAME=mdhlinux
LPATHDIR=/home/mdhlinux/.cache/abrt
MAIL=/var/spool/mail/mdhlinux
PATH=/usr/lib64/qt.3/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/mdhlinux/.local/bin:/home/
mdhlinux/bin
PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
PWD=/home/mdhlinux
QTDIR=/usr/lib64/qt-3.3
QTINC=/usr/lib64/qt-3.3/include
QT_GRAPHICSSYSTEM_CHECKED=1
QT_PLUGIN_PATH=/usr/lib64/kde4/plugins:/usr/lib/kde4/plugins
SHELL=/bin/bash
SHLVL=1
SINCE=1612950701
SSH_CLIENT='192.168.20.37 49418 22'
SSH_CONNECTION='192.168.20.37 49418 192.168.20.41 22'
SSH_TTY=/dev/pts/0
TERM=xterm
UID=1023
USER=mdhlinux
Built-In shell variables and commands
The shell has several built-in commands and variables. Built-in commands and shell variables increase overall
performance of the shell scripts and its capability to access its internal data structure.
In some instances, the echo command is built into the shell, so you will not find an executable called echo on your
disk. Some commands such as those loop constructs (example for command) is almost guaranteed to be built in to
the shell, and consequently you will not find an executable for it.
Summary
- We can customize our environment by setting your shell’s environment variables, in addition to making life easier
for yourself with simple names for extensive commands.
- The curly braces provide a faster way of creating new variables.
- We can re-evaluate a command line using the eval command to parse newly created variables.
- It’s possible to do simple mathematical operations in the shell by sing the expr command. These operations are
limited to integers only.
- The backslash character provides the means to pass arguments with special meaning to the shell without
evaluation to the command.
- Some shell variables and commands are built into the shell to save the time required to load them from disk.