Shell variables give us a place to store values for use by the system, our shell, shell scripts or by programs we run. Each session in UNIX has a set of variables that collectively are referred to as the environmental variables. These variables tell the system where to find applications and documentation, where the userâ€™s home directory is, the current working directory and much more. You can easily view all the environmental variables in the current session with the env command:
SSH_CLIENT=192.168.2.1 54620 22
SSH_CONNECTION=192.168.2.1 54620 192.168.2.100 22
These are some of the default variables provided by the system. As Oracle users weâ€™re also familiar with shell variables such as
While most of the environmental variables in the output above are set by the system, variables like
$ORACLE_HOME need to be set by the user. In the bourne and bash shells, environmental variables are set by giving the variable name, the equal sign (
=) and then the value the variable should be set to. The variable must then be exported with the export command so it can become available to subsequent commands.
$ export ORACLE_HOME
$ echo $ORACLE_HOME
When setting variables you simply use the variable name; however, as we see in the above example we use the
$ prefix to retrieve the value of a variable. You may also see the variable definition followed by the export on the same line separated by a semicolon. The semicolon marks the end of the first command allowing the commands to be executed as if they were on separate lines.
These exported environmental variables typically affect how UNIX and Linux behave or how other commands run. Though not a rule, environmental variable names are typically all uppercase. Lowercase variable names are typically used for local shell variables that are only needed in the current session or script. Local shell variables need not be exported.
$ echo $day
Itâ€™s important to remember that shell variables are specific to a session, not a user. That means if you change the environment variables in one session it will not have any effect on other active sessions.
Beyond the familiar Oracle related shell variables weâ€™ll be using shell variables for storing date information, file and directory names, passwords and much more.
For more information like this check out my book Oracle Shell Scripting, only $34.95 from Rampant TechPress.
Buy it now!