Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

09Shell Variables

Uploaded by

tamilavatar06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

09Shell Variables

Uploaded by

tamilavatar06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Shell Variables

This covers shell variables and introduces user environment. We will discuss the basics of Shell programming language.

- Shell Variables

- Choosing variable names

- Working with shell variables

- Substitution operators

- Local versus environment variables

- Special Shell Variables

- Evaluating Shell Variables

- Simple math operations

- Built-in shell variables


Shell Variables

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

We can assign value to variables using the following syntax:


Variable=value
Note that there are no spaces on either side of the equal sign.
$ count = 2 - wrong
$ count =2 - wrong
$ count= 2 - wrong
$ count=2 - correct
Sets the value of the shell variable count to 2.
To check the value of the variable, use the echo command:
$ count=2
$ echo count
count
$ echo $count
2
The first line set the value of count to 2.
The next command requested the shell to echo the string count.
The third command requested the shell to echo the value of the shell variable count.
We preceded the variable with a $ to ask the shell to specify that we wanted the value of the variable. The $ tells the
shell that the strings after the $ character are the name of a variable and the shell uses the value assigned to this variable.
Choosing Variable Names
When choosing variable names, some rules must be followed. Punctuation characters cannot be used. So, variable name
of t.n.t is not a valid name. If you have to separate words, use the underscore character, as t_n_t.

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 it executes a command.

$ echo $count $tempdir


2 /home/mdhlinux/shellprog/basics

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:

$ ls $unknown –al $unknown *.c


Will be reduced to
$ ls -al *.c
All the NULL values for the unknown variable are removed. If the values of unknown had been set to something, the
command may not have worked.

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

[mdhlinux@localhost ~]$ ls -al $NOTHING $VOOT


ls: cannot access bad: No such file or directory
/:
total 316
dr-xr-xr-x. 18 root root 4096 Feb 10 15:18 .
dr-xr-xr-x. 18 root root 4096 Feb 10 15:18 ..
-rw-r--r--. 1 root root 0 Feb 4 2017 1
lrwxrwxrwx. 1 root root 7 Feb 4 2017 bin -> usr/bin
dr-xr-xr-x. 4 root root 4096 Feb 4 2017 boot
drwxr-xr-x. 22 root root 3620 Feb 10 15:17 dev
drwxr-xr-x. 150 root root 8192 Feb 10 15:17 etc
drwxr-xr-x. 23 root root 4096 Jan 7 16:27 home
lrwxrwxrwx. 1 root root 7 Feb 4 2017 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Feb 4 2017 lib64 -> usr/lib64
drwxr-xr-x. 5 root root 45 Nov 5 2016 opt
dr-xr-x---. 21 root root 4096 Dec 15 13:43 root
lrwxrwxrwx. 1 root root 8 Feb 4 2017 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Nov 5 2016 srv
dr-xr-xr-x. 13 root root 0 Feb 10 15:16 sys
drwxrwxrwt. 10 root root 4096 Feb 10 15:18 tmp
drwxr-xr-x. 13 root root 4096 Feb 4 2017 usr
drwxr-xr-x. 21 root root 4096 Feb 10 15:16 var
[mdhlinux@localhost ~] $
In the above example the VOOT variable is first set to point to the root /. Then you issue the ls command to find the file
with value of $NOTHING, called bad. After that, it proceeds to provide long listing of the files listed in the directory
specified by $VOOT, the / directory.
Setting shell variables as synonyms for arguments is an effective and very powerful way of selectively passing parameters
to a command.
If you are using a particular set of parameters to a command, you could set a variable to that set of flags and save yourself
some typing effort. For example, when compiling a C program in LINUX, you might have to set four options for every
compile: -g, -m486, -ansi and –v.
So the command to type for every source file would be as follows:
$ gcc -g -m486 -v myfile1.c -o myfile1
Typing this every time would be difficult and prone to error. The ideal solution would be to make a shell script out of it.
If you were to change one of the flag, you would have to change every shell script to reflect the new flag. Instead, it
would be faster to set up environment variable once and have all the commands and shell script refer to it.
$ CFLAGS=”-g –m486 –ansi –v”
$ gcc $CFLAGS myfile1.c –o myfile1.out
The verbose command gave the verbose listing, whereas the shell executed the compiler command, gcc.
This variable is set only for the duration of your current session. When you work with customizing your environment, you
learn of ways to save this information for all subsequent sessions in a .profile file. The .profile file is the standard file to
which the shell saves such parameters.

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 ~]$ value2=$value1


[mdhlinux@localhost ~]$ echo $value2
88
[mdhlinux@localhost gamma]$ s=/home/mdhlinux/lab/alpha/beta/gamma
[mdhlinux@localhost gamma]$ d=/home/mdhlinux/shellProg/basics/
[mdhlinux@localhost gamma]$ echo $s
/home/mdhlinux/lab/alpha/beta/gamma
[mdhlinux@localhost gamma]$ echo $d
/home/mdhlinux/shellProg/basics/
[mdhlinux@localhost gamma]$ ls $d
output prog1 prog2 prog2a prog3 sage sams sane sate sims site sort.dat sum.sh task.sh test.cpp

[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.

To achieve this, use the following:


$ mv $BUGSYS ${BUGSYS}.old

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.

Some interesting environment variables include the following:


- HOME: This is your home directory. This is the directory that the command cd sends you to if you do not specify
any parameters.
- SHELL: When LINUX programs invoke a shell command, they check this variable to see what program to invoke. For
example, in the vi editor, if you type !ls at the colon prompt, vi will start up a new copy of the shell specified in the
SHELL environment variable. After typing exit from this new shell, you return to the vi application that you were
originally in.
- TERM: This defines the type of terminal you are using. It is required to service your keyboard input and screen
output. By setting the TERM variable, you are ensuring that your current session will work. Common values for this
variable are vt100 and vt102. By setting the TERM value to an incompatible terminal type, you run the risk of locking
out a terminal from the shell.
- MAIL: This specifies the directory containing mailbox for UNIX’s mailbox facility. You can change this variable to
point to another existing file location without really harming anything.
- USER: This the predefined variable used to specify your account name and is set by the login command.
- LOGNAME: This is another name for USER.

$ 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.

You might also like