I recently met a situation where I had to set some variables in Linux. After some digging, I found several ways to create or set variables, and they all behave differently. I also found that there are several ways to look at the contents of variables, and again, they all behave a bit differently. So I have written them down on this page.
This is all based on SuSe Linux 11.0, with the Gnome desktop, but much of this will be relevant to other Linux flavours as well.
This is based on the Gnome Terminal command line shell - which I think is a bash shell, but it may be a modified bash shell.
To create a new variable, just declare it with its value :-
dog=1
The variable value can be a path to a folder -
dog=/usr/bin
and this works, for example, in the "cd" command -
cd $dog
The downside of creating variables in this way is that they are volatile - kill the shell. and the variable dies with it.
An additional problem with the Gnome Terminal command line shell is that the variable will only work in that shell - now the bash shell command set includes the "export" command, which should allow the variable to also work in other shells, but as far as I can see, this doesn`t work for the Gnome Terminal shell - it does work in scripts however.
If a variable is required to be permanent, then it can be declared in a file - in particular - the "/home/<username>/.bashrc" file.
I haven`t seen any information as to whether a variable that is declared in the "/home/<username>/.bashrc" file should be exported - probably it should be, but more on this further down the page.
cat=1 export cat
This not only makes the variable available after every login, but it is available for all shells.
The "/home/<username>/.bashrc" file is a hidden file, and is not see-able by non-root users using the Gnome file manager ( Nautilus ) or using the Gnome Terminal. So it has to be set by a root user.
However a possible downside of this method of declaring a variable is that the variable is only available to the particular user.
And so ....
Most of the variables that are created during the Linux start-up are set up through the file "/etc/profile", which is read during start-up.
However it is not recommended that this file is subsequently modified for locally required variables. Instead, the recommended method is to create a new file "/etc/profile.local", and to declare the variable in this file. The declaration is just the same as when working at the command line.
However it is required to "export" the variable, so that it is now globally available -
cow=1 export cow
It is neccessary to do a restart after creating or modifying the "/etc/profile.local" file, because it only gets read at start-up.
I have so far found three ways to look at the contents of a variable, and they work differently, depending on whether the variable was exported either via the command line, or in the script in which the variable is declared.
The three ways are three different commands :-
echo $dog
env
set | less
So that is all a bit confusing !
One of the types of variable that I have had to deal with is variables that contain paths to directories. The "PATH" command is one of them, but it certainly is not the only one.
The declaration would typically look like
path=/bin:/sbin:/usr/bin:/usr/sbin
where the different possible paths are seperated by colons, and the system looks in the specified directories in the order in which they are listed in the declaration.
A path which may be useful is the current directory, which is defined with a "." - so the path declaration might become
path=.:/bin:/sbin:/usr/bin:/usr/sbin
and the system would look in the current directory first.
Another useful technique is to add an additional path to the existing variable contents, without relisting all the existing paths.
path=/stuff:$path OR path=$path:/stuff
depending on whether you want to add the new path to the beginning or the end of the existing list of paths.
By convention, variable names usually use all upper case letters, but it is not essential - names using lower case letters work fine. Linux is case sensitive, so "DOG", "dog" and "Dog" would all be different variables.
However there are some exceptions to this - if you change the value in a variable called "path", it also changes the value in the variable "PATH". Also the other way round. I think there are some others that do the same thing.
Some texts suggest that system environment variables should be named in upper case letters, and shell variables should be named in lower case letters.
I am not sure why you would want to do it, but variable names can start with "_" - ie, the underscore character.
The two lines of instructions such as -
cat=1 export cat
can be shortened to
export cat=1
but only in the bash shell, it doesn`t work in other types of shell.
It should be possible to create variables using the "set" command, however I haven`t been able to do it using Gnome Terminal.
You can use the "source" command to force a reread of the files after you have added variables to them - this saves having to log out, or do a restart.
source /etc/profile.local OR source /home/<username>/.bashrc
There may be other ways of declaring variables in Linux, or other ways to look at the contents of variables, but these are the ones I have found so far.