- If you have just started working on Unix environment, and progressing towards writing your own shell scripts.
- If you are a Production support professional trying to reverse engineer your shell script .
What will you achieve after reading this:
7 Unix shell variables used very often in real life unix scripting.
Unix shell variable s, In short
Every environment will have its own set of Environment variables assigned to a certain value. This helps us in exploring and understanding, How the environment is responding to your course of actions in that Environment.
- Unix Environment variables help you in determining how Unix is responding / reacting to your command or set of commands.
- Some of the Environment variables allow you to initialize and use their values whenever are needed.
- some help you to fetch information from Unix Environment.
Now we will see some of the Unix shell variables. These Unix shell variables fetch information for you and predominantly used in Unix shell scripts.
$# Number of arguments
- This command is used to check if the required number of arguments are supplied to the script during during execution of an Unix script
$? Exit value of the last command
- This command is used to know if the previously executed command or script is successfully executed or not
- If the value is zero, the previously executed command is successful, else if the return value is one the status of last executed command is failure
$$ Process Id of the current process
- This is basic information but a support professional will found it very much useful.
- Using this variable, you can find the process number and maintain the script execution audit for an application.
- Sometimes you will want to know process number of a script to force kill the execution if any job is running for exceptionally longer periods.
- As a developer and designer, I would love to know my process number to postfix the process number with the log files.
$n Argument supplied , where n can be from 1 till 9
- $1, $2,…., $9 are meant to read the parameters supplied to the script.
- $1 is the first parameter, $2 is the second parameter and so on.
- You can pretty much use them to assign these values to a local variable of a shell script. For example, if $1 is the fresh inbound file to be loaded, you can assign it to a named variable as below – $FILE_TO_BE_PROCESSED=$1
$0 Name of the Script
- Name of the script can pretty much be used in creating log file, error file names.
- Can also be used to create audit entries of the jobs processed.
$* All arguments supplied (“$1 $2 … $9”)
- It lists all the arguments that are supplied to the script in array form.
$@ All arguments supplied, Each argument separately double quoted (“$1” “$2” … “$9”)
It lists all the arguments that are passed to the script in set form.