programs launched from
the shell (including other shells). For these variables to be
available, they must be exported , at which time
they become environment variables . Environment
variables are passed on to programs and other shells, and together
they are said to form the environment in which
the programs execute.
PATH
is
always made into an environment variable. Exporting a shell variable
to turn it into an environment variable is done using the export command:
$ export MYVAR
Do not include a preceding dollar sign when defining or
exporting a variable (because in this command, you don’t want the
shell to expand the variable to its value). When a variable is
exported to the environment, it is passed into the environment of all
child processes. That is, it will be available to all programs run by
your shell. Here is an example that displays the difference between a
shell variable and an environment variable:
$ echo $MYVAR
No output is returned, because the variable has not been
defined. We give it a value, and then echo its value:
$ MYVAR="hello" $ echo $MYVAR hello
We’ve verified that the variable
MYVAR
contains the value “hello”. Now we
spawn a subshell (or child process) and check the value of this
variable:
$ bash $ echo $MYVAR
Typing bash spawned another copy of the
bash shell. This child process is now our current environment, and as
you can see from the blank line that ends the example, the variable
MYVAR
is not defined here. If we
return to our parent process and export the variable, it becomes an
environment variable that can be accessed in all child
processes:
$ exit $ export MYVAR $ bash $ echo $MYVAR hello
Typing the export command without any
arguments will display all of the exported environment variables
available to your shell. The env command will accomplish the
same thing, just with slightly different output.
Along the same lines are the bash built-in commands set and unset . The command set with no arguments will display of list of
currently set environment variables. The command unset will allow you to clear the value of an
environment variable (assuming it is not read-only). The set command also gives you the ability to change
the way bash behaves. The following are some
examples of using set to modify your interactive
shell.
To change to vi -style editing mode:
$ set –o vi
This example automatically marks variables that are modified or
created for export to the environment of subsequent commands:
$ set –o allexport
To view the current settings for the variables that set can modify, run set
–o .
Entering commands at the command prompt
Commands issued to the shell on a Linux system generally consist
of four components :
A valid command (a shell built-in, program, or script found
among directories listed in the
PATH
, or an explicitly defined
program)
Command options, usually preceded by a dash
Arguments
Line acceptance (i.e., pressing the Enter key), which we
assume in the examples
Each command has its own unique syntax, although most follow a
fairly standard form. At minimum, a command is
necessary:
$ ls
This simple command lists the contents of the current working
directory. It requires neither options nor arguments. Generally, options are letters or words
preceded by asingle ordouble dash and are added after the command and
separated from it by a space:
$ ls -l
The -l option modifies the behavior of ls by listing files in a longer,
more detailed format. In most cases, single-dash options can be either
combined or specified separately. To illustrate this, consider these
two equivalent commands:
$ ls -l -a $ ls -la
By adding the -a option, ls displays files beginning with a dot (which it
hides by default). Adding that option by specifying -la yields the same result. Some commands offer
alternative forms for the same option. In the preceding example, the -a option
can be replaced with
Simon R. Green
Ginny Sterling
J. A. Jance
Anthony Doerr
Bev Pettersen
authors_sort
John Passarella
Brenda Joyce
Robin Perini
Susan Bernhardt