Linux Tutorial

Linux shell variables

script1adsense2code
script1adsense3code

In this session, we have covered how to manage environment variables in the shell. These variables are often needed by applications.

Creating variables

Variables can be created either at the shell or in shell-scripts. Any variable created within a shell script is lost when the script stops executing. A variable created at the prompt, however, will remain in existence until the shell in terminated. The syntax for creating a variable is :

<variable name> = <value>

Note : There will be no spaces on either side of the assignment operator ('=' ).

Following example creates the variable $MyVar and sets its value. It then uses echo (the command is used to display messages on the screen.) to verify the value.

[email protected]:~/test2$ MyVar=999
[email protected]:~/test2$ echo $MyVar
999
[email protected]:~/test2$

case sensitive

This example shows that shell variables are case sensitive!

[email protected]:~/test2$ echo Ram $USER
Ram datasoft
[email protected]:~/test2$ echo Ram $user
Ram
[email protected]:~/test2$

quotes

Notice that double quotes still allow the parsing of variables, whereas single quotes prevent this.

[email protected]:~/test2$ MyVar=999
[email protected]:~/test2$ echo $MyVar
999
[email protected]:~/test2$ echo "$MyVar"
999
[email protected]:~/test2$ echo '$MyVar'
$MyVar
[email protected]:~/test2$ 

The bash shell will replace variables with their value in double quoted lines, but not in single quoted lines.

[email protected]:~/test2$ city=Delhi
[email protected]:~/test2$ echo "We are in $city today."
We are in Delhi today.
[email protected]:~/test2$ echo 'We are in $city today.'
We are in $city today.
[email protected]:~/test2$

dollar sign($)

Another important character interpreted by the shell is the dollar sign $. The shell will look for an environment variable named like the string following the dollar sign and replace it with the value of the variable (or with nothing if the variable does not exist).

These are some examples using $HOSTNAME, $USER, $UID, $SHELL, and $HOME.


[email protected]:~/test2$ echo This is the $SHELL Datasoft
This is the /bin/bash Datasoft
[email protected]:~/test2$ echo This is the $SHELL on computer $HOSTNAME
This is the /bin/bash on computer datasoft-linux
[email protected]:~/test2$ echo The userid of $user is $UID
The userid of is 1000
[email protected]:~/test2$ echo My homedir is $HOME
My homedir is /home/datasoft
[email protected]:~/test2$ 

set

You can use the set command to display a list of environment variables. On Ubuntu and Debian systems, the set command will also list shell functions after the shell variables. Use set | more to see the variables then.

unset

Use the unset command to remove a variable from your shell environment.

[email protected]:~/test2$ MyVar=1234
[email protected]:~/test2$ echo $MyVar
1234
[email protected]:~/test2$ unset MyVar
[email protected]:~/test2$ echo $MyVar

[email protected]:~/test2$

$PS1

The $PS1 variable determines your shell prompt. You can use backslash escaped special characters like \u for the username or \w for the working directory. The bash manual has a complete reference.

In this example we change the value of $PS1 a couple of times.


[email protected]\c:~/test2$ PS1=Prasanta
Prasanta
PrasantaPS1='prompt'
promptPS1='prompt '
prompt PS1='> '
<
<PS1='\[email protected]\h$ '
[email protected]$ PS1='\[email protected]\c:\w$'
[email protected]\c:~/test2$

To avoid unrecoverable mistakes, you can set normal user prompts to green and the root prompt to red. Add the following to your .bashrc for a green user prompt:

# color prompt by Datasoft
RED='\[\033[01;31m\] '
WHITE='\[\033[01;00m\] '
GREEN='\[\033[01;32m\] '
BLUE='\[\033[01;34m\] '
export PS1="${debian_chroot:+($debian_chroot)}$GREEN\[email protected]$BLUE\h$WHITE\w\$ "

$PATH

The $PATH variable is determines where the shell is looking for commands to execute (unless the command is builtin or aliased). This variable contains a list of full path-names of the directories, separated by colons.

[email protected]\c:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

The shell will not look in the current directory for commands to execute! (Looking for executables in the current directory provided an easy way to hack PC-DOS computers). If you want the shell to look in the current directory, then add a . at the end of your $PATH.

[email protected]\c:~$PATH=$PATH:
[email protected]\c:~$echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:
[email protected]\c:~$

Your path might be different when using su instead of su - because the latter will take on the environment of the target user. The root user typically has /sbin directories added to the $PATH variable.

env

The env command without options will display a list of exported variables. The difference with set with options is that set lists all variables, including those not exported to child shells. But env can also be used to start a clean shell (a shell without any inherited environment). The env -i command clears the environment for the subshell.

Notice in this screenshot that bash will set the $SHELL variable on startup.

[email protected]\c:~$ bash -c 'echo $SHELL $HOME $USER'
/bin/bash /home/datasoft datasoft
[email protected]\c:~$ env -i bash -c 'echo $SHELL $HOME $USER'
/bin/bash
[email protected]\c:~$

You can use the env command to set the $LANG, or any other, variable for just one instance of bash with one command. The example below uses this to show the influence of the $LANG variable on file globbing (see the chapter on file globbing).

[email protected]\c:~$ env LANG=en_US.UTF-8 bash -c 'ls File[a-z]'
FileA  FileB
[email protected]\c:~$

export

You can export shell variables to other shells with the export command. This will export the variable to child shells.

 datasoft @ datasoft-linux ~$ var1=Ram
 datasoft @ datasoft-linux ~$ var2=Shyam
 datasoft @ datasoft-linux ~$ export var2
 datasoft @ datasoft-linux ~$ echo $var1 $var2
Ram Shyam
 datasoft @ datasoft-linux ~$ bash
 datasoft @ datasoft-linux ~$ echo $var1 $var2
Shyam

But it will not export to the parent shell (previous screenshot continued).

datasoft @ datasoft-linux ~$ export var3=Madhu
 datasoft @ datasoft-linux ~$ echo $var1 $var2 $var3
Shyam Madhu
 datasoft @ datasoft-linux ~$ exit
exit
 datasoft @ datasoft-linux ~$ echo $var1 $var2 $var3
Ram Shyam madhu
 datasoft @ datasoft-linux ~$

delineate variables

Until now, we have seen that bash interprets a variable starting from a dollar sign, continuing until the first occurrence of a non-alphanumeric character that is not an underscore. In some situations, this can be a problem. This issue can be resolved with curly braces like in this example.

datasoft @ datasoft-linux ~$ prefix=Good
 datasoft @ datasoft-linux ~$ echo very $prefixman and $prefixgirl
very and
 datasoft @ datasoft-linux ~$ echo very ${prefix}man and ${prefix}girl
very Goodman and Goodgirl
 datasoft @ datasoft-linux ~$ 

unbound variables

The example below tries to display the value of the $MyVar variable, but it fails because the variable does not exist. By default the shell will display nothing when a variable is unbound (does not exist).

 datasoft @ datasoft-linux ~$ echo $MyVar

 datasoft @ datasoft-linux ~$

There is, however, the nounset shell option that you can use to generate an error when a variable does not exist.

datasoft @ datasoft-linux ~$ set -u
 datasoft @ datasoft-linux ~$ echo $Myvar
bash: Myvar: unbound variable
 datasoft @ datasoft-linux ~$ set +u
 datasoft @ datasoft-linux ~$ echo $Myvar

 datasoft @ datasoft-linux ~$

In the bash shell set -u is identical to set -o nounset and likewise set +u is identical to set +o nounset.

shell embedding

Shells can be embedded on the command line, or in other words, the command line scan can spawn new processes containing a fork of the current shell. You can use variables to prove that new shells are created. In the screenshot below, the variable $var1 only exists in the (temporary) sub shell.

datasoft @ datasoft-linux ~$ echo $var6

 datasoft @ datasoft-linux ~$ echo $ {var6=6;echo $var6}
$ {var6=6
}
 datasoft @ datasoft-linux ~$ echo $var6

 datasoft @ datasoft-linux ~$

You can embed a shell in an embedded shell, this is called nested embedding of shells.

This screenshot shows an embedded shell inside an embedded shell.

datasoft @ datasoft-linux ~$ A=shell
 datasoft @ datasoft-linux ~$ echo $C$B$A ${B=sub;echo $C$B$A; echo ${C=sub;echo >$C$B$A}}
shell sub;echo shell; echo sub;echo >shell
 datasoft @ datasoft-linux ~$

backticks

Single embedding can be useful to avoid changing your current directory. The screenshot below uses backticks instead of dollar-bracket to embed.

datasoft @ datasoft-linux ~$ echo cd /etc; ls -d * | grep pass
cd /etc
 datasoft @ datasoft-linux ~$

You can only use the $() notation to nest embedded shells, backticks cannot do this.

backticks or single quotes

Placing the embedding between backticks uses one character less than the dollar and parenthesis combo. Be careful however, backticks are often confused with single quotes. The technical difference between ' and ` is significant!

datasoft @ datasoft-linux ~$ echo 'cd /etc; ls -d * | grep pass'
cd /etc; ls -d * | grep pass
 datasoft @ datasoft-linux ~$

shell options

Both set and unset are builtin shell commands. They can be used to set options of the bash shell itself. The next example will clarify this. By default, the shell will treat unset variables as a variable having no value. By setting the -u option, the shell will treat any reference to unset variables as an error. See the man page of bash for more information.

datasoft @ datasoft-linux ~$ echo $varabc

 datasoft @ datasoft-linux ~$ set -u
 datasoft @ datasoft-linux ~$ echo $varabc
bash: varabc: unbound variable
 datasoft @ datasoft-linux ~$ set +u
 datasoft @ datasoft-linux ~$ echo $varabc

 datasoft @ datasoft-linux ~$ 

To list all the set options for your shell, use echo $-. The noclobber (or -C) option will be explained later in this tutorial (in the I/O redirection chapter).

datasoft @ datasoft-linux ~$ echo $-
himuBH
 datasoft @ datasoft-linux ~$ set -C ; set -u
 datasoft @ datasoft-linux ~$ echo $-
himuBCH
 datasoft @ datasoft-linux ~$ set +C ; set +u
 datasoft @ datasoft-linux ~$ echo $-
himBH
 datasoft @ datasoft-linux ~$

When typing set without options, you get a list of all variables without function when the shell is on posix mode. You can set bash in posix mode typing set -o posix.

Exercise, Practice and Solution:

1. Use echo to display Hello followed by your username. (use a bash variable!)

Code:

echo Hello $USER

2. Create a variable answer with a value of 82.

Code:

answer=82

3. Copy the value of $LANG to $MyLANG.

Code:

MyLANG=$LANG

4. List all current shell variables.

Code:

set
set|more on Ubuntu/Debian

5. List all exported shell variables.

Code:

env

6. Do the env and set commands display your variable ?

Code:

env | more
set | more

7. Destroy your answer variable.

Code:

unset answer

8. Create two variables, and export one of them.

Code:

var1=1; export var2=2

9. Display the exported variable in an interactive child shell.

Code:

bash
echo $var2

10. Create a variable, give it the value 'Dumb', create another variable with value 'do'. Use echo and the two variables to echo Dumbledore.

Code:

varx=Dumb; vary=do
echo ${varx}le${vary}re
solution by Yves from Dexia : echo $varx'le'$vary're'
solution by Erwin from Telenet : echo "$varx"le"$vary"re

11. Find the list of backslash escaped characters in the manual of bash. Add the time to your PS1 prompt.

Code:

PS1='\t \[email protected]\h \W$ '

Previous: Linux Commands and arguments
Next: Linux Shell history

New Content: Composer: Dependency manager for PHP, R Programming


script1adsense4code
script1adsense5code
disqus2code
script1adsense6code
script1adsense7code
script1adsense8code
buysellads2code