Variables

From Linux Shell Scripting Tutorial - A Beginner's handbook
Revision as of 00:55, 21 September 2011 by 125.16.142.162 (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
← Chapter 3:The Shell Variables and EnvironmentHomeAssign values to shell variables →

You can use variables to store data and configuration options. There are two types of variable as follows:

System Variables

Created and maintained by Linux bash shell itself. This type of variable (with the exception of auto_resume and histchars) is defined in CAPITAL LETTERS. You can configure aspects of the shell by modifying system variables such as PS1, PATH, LANG,HISTSIZE,and DISPLAY etc.

View All System Variables

To see all system variables, type the following command at a console / terminal:

set

OR

env

OR

printenv

Sample Outputs from set command:

BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="3" [1]="2" [2]="39" [3]="1" [4]="release" [5]="i486-pc-linux-gnu")
BASH_VERSION='3.2.39(1)-release'
COLORTERM=gnome-terminal
COLUMNS=158
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-FSGj0JzI4V,guid=7f59a3dd0813f52d6296ee404a9a68e1
DESKTOP_SESSION=gnome
DIRSTACK=()
DISPLAY=:0.0
EUID=1000
GDMSESSION=gnome
GDM_LANG=en_IN
GDM_XSERVER_LOCATION=local
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
GPG_AGENT_INFO=/tmp/gpg-X7NqIv/S.gpg-agent:7340:1
GROUPS=()
GTK_RC_FILES=/etc/gtk/gtkrc:/home/vivek/.gtkrc-1.2-gnome2
HISTFILE=/home/vivek/.bash_history
HISTFILESIZE=500
HISTSIZE=500
HOME=/home/vivek
HOSTNAME=vivek-desktop
HOSTTYPE=i486
IFS=$' \t\n'
LANG=en_IN
LINES=57
LOGNAME=vivek
MACHTYPE=i486-pc-linux-gnu
MAILCHECK=60
OLDPWD=/home/vivek
OPTERR=1
OPTIND=1
ORBIT_SOCKETDIR=/tmp/orbit-vivek
OSTYPE=linux-gnu
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
PIPESTATUS=([0]="0")
PPID=7542
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
PS2='> '
PS4='+ '
PWD=/tmp
SESSION_MANAGER=local/vivek-desktop:/tmp/.ICE-unix/7194
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
SHLVL=1
SSH_AGENT_PID=7339
SSH_AUTH_SOCK=/tmp/ssh-IoFXYh7194/agent.7194
TERM=xterm
UID=1000
USER=vivek
USERNAME=vivek
WINDOWID=18874428
WINDOWPATH=7
XAUTHORITY=/home/vivek/.Xauthority
XDG_DATA_DIRS=/usr/local/share/:/usr/share/:/usr/share/gdm/
XDG_SESSION_COOKIE=186611583e30fed08439ca0047067c9d-1251633372.846960-528440704
_=set
command_not_found_handle () 
{ 
    if [ -x /usr/lib/command-not-found ]; then
        /usr/bin/python /usr/lib/command-not-found -- $1;
        return $?;
    else
        return 127;
    fi
}
mp3 () 
{ 
    local o=$IFS;
    IFS=$(echo -en "\n\b");
    /usr/bin/beep-media-player "$(cat  $@)" & IFS=o
}
genpasswd () 
{ 
    local l=$1;
    [ "$l" == "" ] && l=16;
    tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs
}
xrpm () 
{ 
    [ "$1" != "" ] && ( rpm2cpio "$1" | cpio -idmv )
}

Commonly Used Shell Variables

The following variables are set by the shell:

System Variable Meaning To View Variable Value Type
BASH_VERSION Holds the version of this instance of bash. echo $BASH_VERSION
HOSTNAME The name of the your computer. echo $HOSTNAME
CDPATH The search path for the cd command. echo $CDPATH
HISTFILE The name of the file in which command history is saved. echo $HISTFILE
HISTFILESIZE The maximum number of lines contained in the history file. echo $HISTFILESIZE
HISTSIZE The number of commands to remember in the command history. The default value is 500. echo $HISTSIZE
HOME The home directory of the current user. echo $HOME
IFS The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is <space><tab><newline>. echo $IFS
LANG Used to determine the locale category for any category not specifically selected with a variable starting with LC_. echo $LANG
PATH The search path for commands. It is a colon-separated list of directories in which the shell looks for commands. echo $PATH
PS1 Your prompt settings. echo $PS1
TMOUT The default timeout for the read builtin command. Also in an interactive shell, the value is interpreted as the number of seconds to wait for input after issuing the command. If not input provided it will logout user. echo $TMOUT
TERM Your login terminal type. echo $TERM
export TERM=vt100
SHELL Set path to login shell. echo $SHELL
DISPLAY Set X display name echo $DISPLAY
export DISPLAY=:0.1
EDITOR Set name of default text editor. export EDITOR=/usr/bin/vim
  • Note you may add above variable (export command) to the initialization file located in the home directory of your account such as ~/.bash_profile.

How Do I Display The Value Of a Variable?

Use echo command to display variable value. To display the program search path, type:

echo "$PATH"

To display your prompt setting, type:

echo "$PS1"

All variable names must be prefixed with $ symbol, and the entire construct should be enclosed in quotes. Try the following example to display the value of a variable without using $ prefix:

echo "HOME"

To display the value of a variable with echo $HOME:

echo "$HOME"

You must use $ followed by variable name to print a variable's contents.

The variable name may also be enclosed in braces:

echo "${HOME}"

This is useful when the variable name is followed by a character that could be part of a variable name:

echo "${HOME}work"

Say hello to printf

The printf command is just like echo command and is available under various versions of UNIX operating systems. It is a good idea to use printf if portability is a major concern for you. The syntax is as follows:

printf "$VARIABLE_NAME\n"
printf "String %s" $VARIABLE_NAME
printf "Signed Decimal Number %d" $VARIABLE_NAME
printf "Floating Point Number %f" $VARIABLE_NAME

To display the program search path, type:

printf "$PATH\n"

OR

printf "The path is set to %s\n" $PATH

Sample outputs:

The path is set to /home/vivek/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

User Defined Variables

Created and maintained by user. This type of variable defined may use any valid variable name, but it is good practice to avoid all uppercase names as many are used by the shell.

← Chapter 3:The Shell Variables and EnvironmentHomeAssign values to shell variables →