Environment variables

In Linux (as well as MS-DOS, Windows etc), each program has a number of "environment variables". These are a series of "settings" which can be used to control certain aspects of the system, user preferences, etc. Whenever a program is executed it receives a copy of the environment variables from the process which executes it. So, if you run a program from the bash shell, that program will receive a copy of all the environment variables from the bash shell itself.

In the following example, I am using the "date" program to display the date. However, I am changing the language-code each time. The "export" command is used to set a new value in an environment variable, or to create it if it doesn’t exist.

sandbox@laptop:~ >date
Thu Aug  2 04:35:55 CEST 2001
sandbox@laptop:~ >export LANG=it_IT
sandbox@laptop:~ >date
gio ago  2 04:45:05 CEST 2001
sandbox@laptop:~ >export LANG=mt_MT
sandbox@laptop:~ >date
Ħam Awi  2 04:45:55 CEST 2001

You can use the "set" command to view all your current environment variables. Since there is usually more than one screenful, you might want to pipe that to the "less" command to view it one screen at a time.

You can include the value of any environment variable within a command by preceding it with a dollar sign. For example, the command "ls -l $SHELL" will produce a long-listing of the bash program itself, since the SHELL environment variable contains the location of the current shell.

Among the most important environment variables are HOME, PATH and PS1. The first obviously points to the user’s home directory. It can be used to construct filenames, such as "$HOME/file1.txt".

The PATH

The second variable, PATH, is a list of directory names separated by colons. This is the list of places that bash will look in when searching for the command names you specify. Thus, when you type "ls", bash will start searching in these directories one at a time until it finds an executable file called "ls", and executes it.

You can use "$PATH" within another command to set the PATH to a different value, in order to add a new directory to the PATH. For example, suppose that you create a new directory for programs in your own home directory. You can add this new directory to the PATH so that bash will look inside it too. To do that (assuming your new directory is called "bin"), you could give the following command:

export PATH=$PATH:$HOME/bin

In that command, I am setting the PATH environment variable to the previous value of itself, followed by a colon, and a directory-name formed from the HOME variable and "/bin".

The PS1 prompt

The last environment variables, PS1, tells bash what to display in the prompt. You can play around with the PS1 variable to produce a prompt that you like. While producing this document, for example, I set up the prompt to contain the required HTML code to colour itself grey.

sandbox@laptop:~ >export PS1="I am \u on \h> "
I am sandbox on laptop>

Note however than any changes you make to the environment variables will be temporary. They will revert to normal next time you open a terminal window, or log on. We will see how to make these settings permanent in a moment.

Environment variables

In Linux (as well as MS-DOS, Windows etc), each program has a number of "environment variables". These are a series of "settings" which can be used to control certain aspects of the system, user preferences, etc. Whenever a program is executed it receives a copy of the environment variables from the process which executes it. So, if you run a program from the bash shell, that program will receive a copy of all the environment variables from the bash shell itself.

In the following example, I am using the "date" program to display the date. However, I am changing the language-code each time. The "export" command is used to set a new value in an environment variable, or to create it if it doesn’t exist.

sandbox@laptop:~ >date
Thu Aug  2 04:35:55 CEST 2001
sandbox@laptop:~ >export LANG=it_IT
sandbox@laptop:~ >date
gio ago  2 04:45:05 CEST 2001
sandbox@laptop:~ >export LANG=mt_MT
sandbox@laptop:~ >date
Ħam Awi  2 04:45:55 CEST 2001

You can use the "set" command to view all your current environment variables. Since there is usually more than one screenful, you might want to pipe that to the "less" command to view it one screen at a time.

You can include the value of any environment variable within a command by preceding it with a dollar sign. For example, the command "ls -l $SHELL" will produce a long-listing of the bash program itself, since the SHELL environment variable contains the location of the current shell.

Among the most important environment variables are HOME, PATH and PS1. The first obviously points to the user’s home directory. It can be used to construct filenames, such as "$HOME/file1.txt".

The PATH

The second variable, PATH, is a list of directory names separated by colons. This is the list of places that bash will look in when searching for the command names you specify. Thus, when you type "ls", bash will start searching in these directories one at a time until it finds an executable file called "ls", and executes it.

You can use "$PATH" within another command to set the PATH to a different value, in order to add a new directory to the PATH. For example, suppose that you create a new directory for programs in your own home directory. You can add this new directory to the PATH so that bash will look inside it too. To do that (assuming your new directory is called "bin"), you could give the following command:

export PATH=$PATH:$HOME/bin

In that command, I am setting the PATH environment variable to the previous value of itself, followed by a colon, and a directory-name formed from the HOME variable and "/bin".

The PS1 prompt

The last environment variables, PS1, tells bash what to display in the prompt. You can play around with the PS1 variable to produce a prompt that you like. While producing this document, for example, I set up the prompt to contain the required HTML code to colour itself grey.

sandbox@laptop:~ >export PS1="I am \u on \h> "
I am sandbox on laptop>

Note however than any changes you make to the environment variables will be temporary. They will revert to normal next time you open a terminal window, or log on. We will see how to make these settings permanent in a moment.

Comments

thanks

I find very interesting information at this article, thanks.
buy propecia online

Thanks

I love this site, very useful, thanks.

purchase pills