Wednesday, April 8, 2009

Variables in shell

In linux there are two types of variables. These two types of variable is used in shell as well as shell script.

1)System variables: These variables are created and maintained by linux itself. From shell script we can use these variables in order to different types of helps. System variables are upper case.

2)User defined variables: These types of variables are created and maintained by user. In shell script you define it as yourself in order to manage the code. Normally user defined variables are declared as lowercase.

You can see any system variables or user defined variables by,
echo $VARIABLE_NAME
command. Where VARIABLE_NAME is the variable name that you wish to see.

You can set your system defined variable by export or setenv command based on your shell type.
On bash shell use, export VARIABLE_NAME=value
On c shell use, setenv VARIABLE_NAME=value

In order to see all the system variables defined on the system you can use set command like below.

arju:~# set
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="3" [1]="1" [2]="17" [3]="1" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION='3.1.17(1)-release'
CMN=/usr/unsafe/dev/common
COLUMNS=80
CONF=/etc/apache/httpd.conf
CVS_RSH=ssh
DIRSTACK=()
EDITOR=/usr/bin/vim
EUID=0
GDC=/usr/local/install/gdc
GROUPS=()
G_BROKEN_FILENAMES=1
HISTFILE=/root/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HOME=/root
HOSTNAME=arju
HOSTTYPE=x86_64
IFS=$' \t\n'
INPUTRC=/etc/inputrc
INST=/usr/local/install
ISCMN=/usr/unsafe/dev/isuite/common
KDEDIR=/usr
KDE_IS_PRELINKED=1
KDE_NO_IPV6=1
LANG=en_US.UTF-8
LD_LIBRARY_PATH=:/usr/local/lib
LESSOPEN='|/usr/bin/lesspipe.sh %s'
LINES=24
LOG=/var/log
LOGNAME=root
MACHTYPE=x86_64-redhat-linux-gnu
MAIL=/var/spool/mail/root
MAILCHECK=60
NXDIR=/usr/NX
OLDPWD=/home/arju/test
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/lib64/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/NX/bin:/usr/local/etc/misc:/usr/contrib/bin:/usr/contrib/bin/infosec:/usr/local/sbin:/usr/local/bin:/bin:/usr/local/install/gdc/bin:/usr/local/install/ioncube:/root/bin
PHPLIB=/usr/safe/phplib
PIPESTATUS=([0]="0")
PKGS=/usr/local/packages
PPID=11601
PRELINKING=yes
PRELINK_FULL_TIME_INTERVAL=14
PRELINK_NONRPM_CHECK_INTERVAL=7
PRELINK_OPTS=-mR
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'
PS1='arju:\w# '
PS2='> '
PS4='+ '
PWD=/root
QTDIR=/usr/lib64/qt-3.3
QTINC=/usr/lib64/qt-3.3/include
QTLIB=/usr/lib64/qt-3.3/lib
SAFE=/usr/safe
SETUP=/etc/profile.d/zBiztek
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
SHLVL=1
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
SSH_CLIENT='113.11.14.16 2195 22'
SSH_CONNECTION='113.11.14.16 2195 192.168.100.18 22'
SSH_TTY=/dev/pts/0
TANGO=/usr/local/install/gdc/include/d/4.1.1/tango
TERM=xterm
UID=0
ULEM=/usr/local/etc/misc
USER=root
WEB=/usr/safe/websites
_=echo
consoletype=pty
qt_prefix=/usr/lib64/qt-3.3

Below is the description of major system variables.
1)BASH: Which contains the location of bash shell.
# echo $BASH
/bin/bash

2)BASH_VERSION: Which shows the bash shell version information.
# echo $BASH_VERSION
3.1.17(1)-release

3)COLUMNS: Which contains number of columns for the screen.
# echo $COLUMNS
80

4)HOME: Which contains the home directory location of the user.
# echo $HOME
/root

5)LINES: Which contains the number of columns in the screen.
# echo $LINES
24

6)LOGNAME: Which holds the username who is logging to the system.
# echo $LOGNAME
root

7)OSTYPE: Which holds the OS type information.
# echo $OSTYPE
linux-gnu

8)PATH: PATH variable contains the search path of all binary files. Two different locations are separated by colon (:).
# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

9)PS1: Contains the information about what would be the prompt settings.
arju:/home/arju# echo $PS1
arju:\w#

If you change directory to test your prompt also to be changed.
# cd test
arju:/home/arju/test#

10)PWD: Contains current directory location.
# echo $PWD
/home/arju/test

11)SHELL: Shows information of shell that you are using.
# echo $SHELL
/bin/bash

12)USER: Contains username who is logging.
# echo $USER
root

Note that to see/access variable you have to use prepend "$" with the variable name. But to assign values to the variable you must not prepend "$".

# echo PWD # As we did not prepend $ so PWD is shown on screen.
PWD

Note that on unix/linux, after hash sign (#) rest of things are considered as comments.
Related Documents
http://arjudba.blogspot.com/2009/04/basic-steps-to-write-shell-script.html
http://arjudba.blogspot.com/2009/04/what-is-kernel-shell-shell-script.html
http://arjudba.blogspot.com/2009/04/simple-shell-script-to-check-whether.html

No comments:

Post a Comment