How to use variables in shell scripts

A variable is simply a placeholder for some value. The value can change; however, the variable name will always be the same. Shell variables are capitalized by convention. The shell maintains two lists of variables:
1. Those local to the current shell
2. Those global to all shells (environment variables).

Use the set and env commands to display local and environmental variables, respectively. The following is a partial output of the set and env statements. Many variables appear in both the local and environment variable list.

$ set
BASH=/bin/bash
HISTFILE=/home/user/.bash_history
HISTFILESIZE=100000
HISTSIZE=100000
HOME=/home/user
HOSTNAME=geek.mylabserver.com
....
$ env
TERM=xterm-256color
SHELL=/bin/bash
USER=user
LOGNAME=user
....

Naming conventions for variables

– Shell variables are capitalized by convention.
– Variable names can contain uppercase or lowercase letters, digits, and underscores.
– Variable names cannot begin with a digit.
– Some of the boot scripts show mixed-case variable names. Be sure to use meaningful variable names.

How to create Variables in Shell Scripts

To set a variable in the shell, use the syntax:

var=value
Note – Variables are case-sensitive so, after they are defined, they must later be spelled using exactly the same uppercase and lowercase letters. Variable names have a limit of 255 characters.

Do not place spaces around the = sign. If the value contains spaces or special characters, use single or double quotes; for example:

$ var="varibale with spaces"

Display Variable value

To display the value of a shell variable, use the echo command and place a $ immediately in front of the variable name. You can also display more than one variable in the same line using single echo command. For Example :

$ var1=18
$ var2="Till I Die" 
$ echo $var1 $var2 
18 Till I Die

Special Shell Variables

Several shell variables are available to users. The shell sets the variable value at a process creation or termination.

1. Process Identification

The $$ shell variable is available to users. It contains the current process ID number. Use this variable to create file names that are unlikely to already exist.

$ echo $$
10916

If you change or spawn a new shell, this value of the variable would change as well. For example:

$ echo $$ 
1289 
$sh
$ echo $$ 
17716
$ exit
$ echo $$ 
1289

2. Exit status

The exit status is the integer value of the last executed command (program, script, or shell statement). The ? variable provides the exit status of the most recent foreground process. It:

  • Determines if the process ran successfully.
  • Sets the ? variable to 0 if a process succeeded and to a nonzero value if it didn’t succeed.

Think of this as equating success with zero errors occurring and equating failure with one or more errors occurring or the command not being able to do what was requested.

$ grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
$ echo $?
0
$ grep rot /etc/passwd
$ echo $?
1

3. Background Process Identification

A shell allows you to run a command in the background using the & operator. To find the PID of the last background job started, echo the ! variable.

$ sleep 10 &
[1] 1609
$ echo $!
1609
Related Post