5 Introducing BASH shell variables
A BASH shell variable is a feature that allows you or the shell to store data. This data can be used to provide critical system information or to change the behavior of how the BASH shell (or other commands) work.
Variables are given names and stored temporarily in memory. When you close a terminal window or shell, all of the variables are lost. However, the system automatically recreates many of these variables when a new shell is opened.
To display the value of a variable, you can use the
echo
command. The echo
command is used to display output in the terminal; in the example below, the command will display the value of the HISTSIZE
variable:sysadmin@localhost:~$ echo $HISTSIZE1000sysadmin@localhost:~$
The
HISTSIZE
variable defines how many previous commands to store in the history list. To display the value of the variable, use a dollar sign$
character before the variable name. To modify the value of the variable, you don't use the $
character:sysadmin@localhost:~$ HISTSIZE=500sysadmin@localhost:~$ echo $HISTSIZE500sysadmin@localhost:~$
There are many shell variables that are available for the BASH shell, as well as variables that will affect different Linux commands. A discussion of all shell variables is beyond the scope of this chapter, however more shell variables will be covered as this course progresses.
6 PATH variable
One of the most important BASH shell variables to understand is the
PATH
variable.
The term path refers to a list that defines which directories the shell will look in for commands. If you type in a command and receive a "command not found" error, it is because the BASH shell was unable to locate a command by that name in any of the directories included in the path. The following command displays the path of the current shell:
sysadmin@localhost:~$ echo $PATH/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/gamessysadmin@localhost:~$
Based on the proceeding output, when you attempt to execute a command, the shell will first look for the command in the
/usr/lib/qt-3.3/bin
directory. If the command is found in that directory, then it is executed. If it isn't found, then the shell will look in the /usr/local/bin
directory.
If the command is not found in any directory listed in the
PATH
variable, then you will receive a command not found
error:sysadmin@localhost:~$ zed-bash: zed: command not foundsysadmin@localhost:~$
If custom software is installed on your system, you may need to modify the
PATH
to make it easier to execute these commands. For example, the following will add the /usr/bin/custom
directory to the PATH
variable:sysadmin@localhost:~$ PATH=/usr/bin/custom:$PATHsysadmin@localhost:~$ echo $PATH/usr/bin/custom:/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/gamessysadmin@localhost:~$
7 export Command
There are two types of variables used in the BASH shell, local and environment. Environment variables, such as
PATH
and HOME
, are used by BASH when interpreting commands and performing tasks. Local variables are often associated with user based tasks and are lowercase by convention. To create a local variable, simply type:sysadmin@localhost:~$ variable1='Something'
To view the contents of the variable, refer to it with a leading
$
sign:sysadmin@localhost:~$ echo $variable1Something
To view environment variables, use the
env
command (searching through the output using grep
, as shown here, will be discussed in later chapters). In this case, the search for variable1
in the environment variables results in no output:sysadmin@localhost:~$ env | grep variable1sysadmin@localhost:~$
After exporting
variable1
, it is now an environment variable. Notice that this time, it is found in the search through the environment variables:sysadmin@localhost:~$ export variable1sysadmin@localhost:~$ env | grep variable1variable1=Something
The
export
command can also be used to make an environment variable upon its creation:sysadmin@localhost:~$ export variable2='Else'sysadmin@localhost:~$ env | grep variable2variable2=Else
To change the value of an environment variable, simply omit the
$
when referencing it:sysadmin@localhost:~$ variable1=$variable1' '$variable2sysadmin@localhost:~$ echo $variable1Something Else
Exported variables can be removed using the
unset
command:sysadmin@localhost:~$ unset $variable2
8 which Command
There may be situations where different versions of the same command are installed on a system or where commands are accessible to some users and not others. If a command does not behave as expected or if a command is not accessible that should be, it can be beneficial to know where the shell is finding the command or which version it is using.
It would be tedious to have to manually look in each directory that is listed in the
PATH
variable. Instead, you can use the which
command to display the full path to the command in question:sysadmin@localhost:~$ which date/bin/datesysadmin@localhost:~$ which cal/usr/bin/calsysadmin@localhost:~$
The
which
command searches for the location of a command by searching the PATH
variable.9 type Command
The
type
command can be used to determine information about various commands. Some commands originate from a specific file:sysadmin@localhost:~$ type whichwhich is hashed (/usr/bin/which)
This output would be similar to the output of the
which
command (as discussed in the previous section, which displays the full path of the command):sysadmin@localhost:~$ which which/usr/bin/which
The
type
command can also identify commands that are built into the bash (or other) shell:sysadmin@localhost:~$ type echoecho is a shell builtin
In this case, the output is significantly different from the output of the
which
command:sysadmin@localhost:~$ which echo/bin/echo
Using the
-a
option, the type
command can also reveal the path of another command:sysadmin@localhost:~$ type -a echoecho is a shell builtinecho is /bin/echo
The
type
command can also identify aliases to other commands:sysadmin@localhost:~$ type llll is aliased to `ls -alF'sysadmin@localhost:~$ type lsls is aliased to `ls --color=auto'
The output of these commands indicate that
ll
is an alias for ls -alF
, and even ls
is an alias for ls --color=auto
. Again, the output is significantly different from the which
command:sysadmin@localhost:~$ which llsysadmin@localhost:~$ which ls/bin/ls
The
type
command supports other options, and can lookup multiple commands simultaneously. To display only a single word describing the echo
, ll
, and which
commands, use the -t
option:sysadmin@localhost:~$ type -t echo ll whichbuiltinaliasfile
10 Aliases
An alias can be used to map longer commands to shorter key sequences. When the shell sees an alias being executed, it substitutes the longer sequence before proceeding to interpret commands.
For example, the command
ls -l
is commonly aliased to l
or ll
. Because these smaller commands are easier to type, it becomes faster to run the ls -l
command line.
You can determine what aliases are set on your shell with the
alias
command:sysadmin@localhost:~$ aliasalias egrep='egrep --color=auto'alias fgrep='fgrep --color=auto'alias grep='grep --color=auto'alias la='ls -A'alias l='ls -CF' alias ll='ls -alF'alias ls='ls --color=auto'
The aliases that you see from the previous examples were created by initialization files. These files are designed to make the process of creating aliases automatic and will be discussed in more detail in a later chapter.
New aliases can be created by typing
alias name=command
where name is the name you want to give the alias and command is the command you want to have executed when you run the alias.
For example, you can create an alias so that
lh
displays a long listing of files, sorted by size with a "human friendly" size with the alias lh='ls -Shl'
command. Typing lh
should now result in the same output as typing the ls -Shl
command:sysadmin@localhost:~$ alias lh='ls -Shl'sysadmin@localhost:~$ lh /etc/ppptotal 0drwxr-xr-x 1 root root 10 Jan 29 2015 ip-down.ddrwxr-xr-x 1 root root 10 Jan 29 2015 ip-up.d
Aliases created this way will only persist while the shell is open. Once the shell is closed, the new aliases that you created will be lost. Additionally, each shell has its own aliases, so if you create an alias in one shell and then open another shell, you won't see the alias in the new shell.
Continued on next page : Part III
0 comments