UNIX and Linux shells provide an abundance of useful built-in information that can be referenced in globally available variables. In order to see the information provided in a shell, the set command can be run as demonstrated below.

Here’s a partial output of the set command:

$ set
BASH=/bin/bash
BASH_VERSINFO=([0]="2" [1]="05b" [2]="0" [3]="1" [4]="release" [5]="i386-redhat-linux-gnu")
BASH_VERSION='2.05b.0(1)-release'
GROUPS=()
G_BROKEN_FILENAMES=1
HISTFILE=/home/tclark/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HOME=/home/tclark
HOSTNAME=appsvr.mytec.com
OSTYPE=linux-gnu
PATH=/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/tclark/bin
...
PS1='[\u@\h \W]\$ '
PS2='> '
PS4='+ '
PWD=/home/tclark
SHELL=/bin/bash
SHLVL=1
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
SSH_CLIENT='206.107.231.178 1379 22'
SSH_CONNECTION='206.107.231.178 1379 192.168.15.105 22'
SSH_TTY=/dev/pts/0
SUPPORTED=en_US.UTF-8:en_US:en
TERM=vt100
UID=503
USER=tclark
_=clear

The contents of a shell variable can be displayed by using the echo command and prefacing the variable name with a dollar sign as demonstrated below. Shell variables are referenced using all capital letters.

$ echo $TERM
vt100
$ echo $USER
tclark
$ echo $HOSTNAME ... $LOGNAME
appsvr.mytec.com ... tclark

There are also some special built-in variables that can be useful when creating shell scripts. Some of them are listed in the table below.

Built-in Variable Description
$# The total number of arguments passed to a shell script on the command line.
$* All arguments passed to the shell script.
$0 The command (script) invoked on the command line.
$1 – $9 The first through ninth arguments passed to the shell script from the command line.

These variables are provided by the shell and the names should not be used for other variables.

Easy Linux CommandsFor more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

Buy it now!


linux, unix, system administration, sysadmin