Using Variables in Perl
$_, the default variable. Creating containers for variables in Perl is easy. Give the container a name (which is made up of a combination of the letters A-Z, a-z, the digits 0-9 and the underscore character), then precede the name with one of Perl’s special variable naming characters, depending on what the variable will be used for:
$ – a scalar variable (one of something);
@ – an array variable (a collection of somethings, a list);
% – a hash variable (a collection of name/value pairs); and
\ – a referenced variable (a ‘pointer’ to something else, usually another variable).
Useful yum Variables
The following is a list of variables you can use for both yum commands and yum configuration files (i.e. /etc/yum.conf and .repo files).
$releasever
This is replaced with the package’s version, as listed in distroverpkg. This defaults to the version of the redhat-release package.
$arch
This is replaced with your system’s architecture, as listed by os.uname() in Python.
$basearch
This is replaced with your base architecture. For example, if $arch=i686 then $basearch=i386.
$YUM0-9
This is replaced with the value of the shell environment variable of the same name. If the shell environment variable does not exist, then the configuration file variable will not be replaced.
Reference: Redhat Docs.
Shell Environment Variables
There are specific environment variables that the bash shell uses by default to define the system environment.
The most important environment variable in this list is the PATH environment variable. When you enter a command in the shell command line interface (CLI), the shell must search the system to find the program. The PATH environment variable defines the directories it searches looking for commands.
The PATH environment variable looks like this.
root@dell:~# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
This shows that there are six directories where the shell looks for commands. Each directory in the PATH is separated by a colon. The PATH also shows the order in which it looks for commands.
The individual directories listed in the PATH are separated by a colon. All you need to do is reference the original PATH value, and add any new directories to the string.
$ PATH=$PATH:/home/buddy/bin
Setting global environment variables
Global environment variables are visible from any child processes created by the process that sets the global environment variable. The method used to create a global environment variable is to create a local environment variable, then export it to the global environment.
This is done by using the export command:
Example:
export http_proxy=http://192.168.1.1:3128/
You can also remove an existing environment variable. This is done by using the unset command.
Setting local environment variables
You can set your own environment variables directly from the bash shell.
Once you start a bash shell (or spawn a shell script), you’re allowed to create local variables that are visible within your shell process. You can assign either a numeric or a string value to an
environment variable by assigning the variable to a value using the equal sign:
root@dell:~# process=testing
root@dell:~# echo $process
testing
Now any time you need to reference the value of the test environment variable, just reference it by the name $process.