Linux Shell built-in Variables Every System Admin Should Know

There are many shell built-in variables which are used for administrating and writing shell scripts. In this post we will see important built-in variables and their importance. Please add any other built-ins which are useful for administration and for shell scripting.

Note: in order get present values of these variables use echo command to get them. For example if you want to see present shell then you can give echo $SHELL to get the value.

$SHELL –Gives present shell
$HISTSIZE –Gives the size of number of Commands that can be accommodated in History file
$HISTFILE –To get the history file location
$USER –Displays username
$EUID –Display UserID.
$GROUPS – To get GID info.
$PWD –Display’s present working Directory.
$HOSTNAME –Displays hostname.
$HOME –Give you user’s home directory.
$HOSTTYPE or $MACHTYPE – To get host architecture ie 32bit or 64 bit.
$OSTYPE –Detect OS, such as gnu Linux, sun sol etc. Useful for controlling the shell script flow.
$TERM –Gives you terminal name.
$TMOUT –Exit the shell, if no activity was there on terminal, useful for securing the server.
$PATH –Get path to all the binaries ie commands
$PIPESTATUS –To get exit status of piped output.
$BASH_VERSION –To get bash version. Useful for controlling the shell script flow.
$PPID –Get parent process ID.
$PS1, $PS2, $PS3, $PS4 –Different prompts. These are useful for menu driven scripts.
In Scripting
$RANDOM –To get a random number, very much useful for giving random passwords for new users.
$LINENO –To get what is the present line number the script is executing. Useful for debugging a shell script.
$REPLY –REPLY holds last read value.
$SECONDS –To get number of seconds the script is running.
Positional parameters: $1, $2, $3, $4, $5, $6, $7, $8, $9
$0 –Gives script name
$* –All positional parameters
$@ All positional parameters with spaces
$# –Number of Parameters
$$ –Current Process ID
$! –ID of Background job
$? –Error or exit status
$_ –Get previous command last argument

0 comments:

Post a Comment