All programming languages use variables to retain data for later use or modification. Unlike compiled languages, most scripting languages do not require a type declaration before a variable is created. The type is determined by usage. The value of a variable is accessed by preceding the variable name with a dollar sign. The shell defines several variables it uses for configuration and information like available printers, search paths, and so on. These are called environment variables.
Using variables and environment variables
Getting ready
Variables are named as a sequence of letters, numbers, and underscores with no whitespace. Common conventions are to use UPPER_CASE for environment variables and camelCase or lower_case for variables used within a script.
All applications and scripts can access the environment variables. To view all the environment variables defined in your current shell, issue the env or printenv command:
$> env PWD=/home/clif/ShellCookBook HOME=/home/clif SHELL=/bin/bash # ... And many more lines
To view the environment of other processes, use the following command:
cat /proc/$PID/environ
Set PID with a process ID of the process (PID is an integer value).
Assume an application called gedit is running. We obtain the process ID of gedit with the pgrep command:
$ pgrep gedit 12501
We view the environment variables associated with the process by executing the following command:
$ cat /proc/12501/environ GDM_KEYBOARD_LAYOUT=usGNOME_KEYRING_PID=1560USER=slynuxHOME=/home/slynux
The /proc/PID/environ special file contains a list of environment variables and their values. Each variable is represented as a name=value pair, separated by a null character (\0). This is not easily human readable.
To make a human-friendly report, pipe the output of the cat command to tr, to substitute the \0 character with \n:
$ cat /proc/12501/environ | tr '\0' '\n'
How to do it...
Assign a value to a variable with the equal sign operator:
varName=value
The name of the variable is varName and value is the value to be assigned to it. If value does not contain any space character (such as space), it need not be enclosed in quotes, otherwise it must be enclosed in single or double quotes.
Access the contents of a variable by prefixing the variable name with a dollar sign ($).
var="value" #Assign "value" to var echo $var
You may also use it like this:
echo ${var}
This output will be displayed:
value
Variable values within double quotes can be used with printf, echo, and other shell commands:
#!/bin/bash #Filename :variables.sh fruit=apple count=5 echo "We have $count ${fruit}(s)"
The output will be as follows:
We have 5 apple(s)
Because the shell uses a space to delimit words, we need to add curly braces to let the shell know that the variable name is fruit, not fruit(s).
Environment variables are inherited from the parent processes. For example, HTTP_PROXY is an environment variable that defines which proxy server to use for an Internet connection.
Usually, it is set as follows:
HTTP_PROXY=192.168.1.23:3128 export HTTP_PROXY
The export command declares one or more variables that will be inherited by child tasks. After variables are exported, any application executed from the current shell script, receives this variable. There are many standard environment variables created and used by the shell, and we can export our own variables.
For example, the PATH variable lists the folders, which the shell will search for an application. A typical PATH variable will contain the following:
$ echo $PATH /home/slynux/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
Directory paths are delimited by the : character. Usually, $PATH is defined in /etc/environment, /etc/profile or ~/.bashrc.
To add a new path to the PATH environment, use the following command:
export PATH="$PATH:/home/user/bin"
Alternatively, use these commands:
$ PATH="$PATH:/home/user/bin" $ export PATH $ echo $PATH /home/slynux/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/user/bin
Here we have added /home/user/bin to PATH.
Some of the well-known environment variables are HOME, PWD, USER, UID, and SHELL.
When using single quotes, variables will not be expanded and will be displayed as it is. This means, $ echo '$var' will display $var.
Whereas, $ echo "$var" will display the value of the $var variable if it is defined, or nothing if it is not defined.
There's more...
The shell has many more built-in features. Here are a few more:
Finding the length of a string
Get the length of a variable's value with the following command:
length=${#var}
Consider this example:
$ var=12345678901234567890$ echo ${#var} 20
The length parameter is the number of characters in the string.
Identifying the current shell
To identify the shell which is currently being used, use the SHELL environment variable.
echo $SHELL
Alternatively, use this command:
echo $0
Consider this example:
$ echo $SHELL /bin/bash
Also, by executing the echo $0 command, we will get the same output:
$ echo $0 /bin/bash
Checking for super user
The UID environment variable holds the User ID. Use this value to check whether the current script is being run as a root user or regular user. Consider this example:
If [ $UID -ne 0 ]; then echo Non root user. Please run as root. else echo Root user fi
Note that [ is actually a command and must be separated from the rest of the string with spaces. We can also write the preceding script as follows:
if test $UID -ne 0:1 then echo Non root user. Please run as root else echo Root User fi
The UID value for the root user is 0.
Modifying the Bash prompt string (username@hostname:~$)
When we open a terminal or run a shell, we see a prompt such as user@hostname: /home/$. Different GNU/Linux distributions have different prompts and different colors. The PS1 environment variable defines the primary prompt. The default prompt is defined by a line in the ~/.bashrc file.
- View the line used to set the PS1 variable:
$ cat ~/.bashrc | grep PS1 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
- To modify the prompt, enter the following command:
slynux@localhost: ~$ PS1="PROMPT> " # Prompt string changed PROMPT> Type commands here.
- We can use colored text using the special escape sequences such as \e[1;31 (refer to the Displaying output in a terminal recipe of this chapter).
Certain special characters expand to system parameters. For example, \u expands to username, \h expands to hostname, and \w expands to the current working directory.