Setting and Using Variables on GNU/Linux and *NIX Shells
The specific process used to set variables will depend on the shell
that you are using. For example, GNU/Linux distros such as Debian and
its forks (ex: Ubuntu, Mint, Trisquel, etc.) default to the BASH(1),
or, the GNU Bourne-Again
SHell. This article currently only gives examples for both
the CSH(1)
and BASH(1)
shells. CSH was tested on FreeBSD and BASH was tested on Debian
GNU/Linux. The concepts described should be able to, with some
modifications, work with other system setups.
To create a variable to last during the current shell session only,
you may use "export" to do so from the command line.
-
CSH:
-
set [VAR_NAME] = "[STRING_OR_COMMAND]"
-
SH:
-
export [VAR_NAME] "[STRING_OR_COMMAND]"
For the variable to stay saved for use with multiple instances, or to
persist between logins, you may add it to either your
local .profile file, or to
the /etc/profile file.
To remove a variable, run the unset command from the command
line.
-
CSH & BASH:
-
-
unset [VAR_NAME]
If added to .profile or /etc/profile,
you will also want to delete or comment out the line for that
variable. Commenting out is preferred if you intend to use it again
in the future, or if you want to use it as a reference. At this time,
I am not aware of ways to set variables with spaces in them. The
easier short-term option out of the two I can think of is to put the
variable in quotes. For example:
-
CSH:
-
% cd "$TEST_VAR"
-
SH:
-
$ cd "$TEST_VAR"
The easier long-term option would be to simply remove the space from
the file path, but you will need to ensure that you do not have any
scripts or programs that have the path hardcoded in them, or else bad
things will happen.
Anton McClure /
anton@aperture.nonpaged.com