Setting variables in Linux

 

Introduction

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.

 

Using the command line

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.

 

Creating a non-volatile variable

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 ....

 

Creating a global variable

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.

 

Looking at the contents of variables

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 :-

⇒   The "echo" command
The "echo" command can be used to display the contents of variables that have been declared by any of the above methods, but you need to know the name of the variable, I haven`t found a way of using a wildcard to get a list of all the variables.

        echo $dog

The "echo" command will show the value of variables created in the command line, or created in the "/home/<username>/.bashrc" file, even if the variable has not been "exported". However it will not show variables declared in the "/etc/profile.local" file unless they have been "exported".
⇒   The "env" command
The "env" command will display variables that are declared by any of the above methods, but only if the variable has been "exported" after the declaration.

        env

⇒   The "set" command
The "set"command is not listed as a command available to the Gnome Terminal command line shell, however some pages on the web suggest that it should be available in the bash shell - the manual page on the bash shell available on the www.gnu.org website provides a whole section just for the "set" command, so that certainly suggests that it should work.
It does in fact produce a result, if typed at the command prompt with no arguements. It produces a list of all the variables that have been set - by any of the above methods.
However there is a bit of a problem with it, in that, as well as the list of variables, it also reproduces what looks like a script - all 709 lines of it - which rather clogs up the Gnome Terminal screen. This can be worked around by piping the output of "set" to the "less" command.

        set | less

The "set" command will show the value of variables created in the command line, or created in the "/home/<username>/.bashrc" file, even if the variable has not been "exported". However it will not show variables declared in the "/etc/profile.local" file unless they have been "exported".

So that is all a bit confusing !

 

Variables that contain paths

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.

 

Some footnotes

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.

 

 

 

 

 

website design by ron-t

 

website hosting by freevirtualservers.com

 

© 2024   Ron Turner

 

+                                   +  

 

Link to the W3C website.   Link to the W3C website.