Running Commands from the Shell
computer software
Running Commands from the Shell Running Commands from the Shell
Running Commands from the Shell   Home | Site Map | About Us | Products | Services | News | Contact Us | Links Running Commands from the Shell
Running Commands from the Shell Running Commands from the Shell

Copyright © 2009

Linux® Bible


If you want to have other text right up against the output from an environment variable, you can
surround the variable in braces. This protects the variable name from being misunderstood. For
example, if you want to add a command name to the
AB
variable shown earlier, you can type the
following:
$ echo ${AB}/adventure
/usr/dog/contagious/ringbearer/grind/adventure
Remember that you must export the variable so that it can be picked up by other shell commands.
You must add the export line to a shell configuration file for it to take effect the next time you log
in. The
export
command is fairly flexible. Instead of running the
export
command after you set
the variable, you can do it all in one step, as follows:
$ export XYZ=/home/xyz/bin
You can override the value of any environment variable. This can be temporary, by simply typing
the new value, or you can add the new export line to your
$HOME/.bashrc
file. One useful vari-
able to update is PATH:
$ export PATH=$PATH:/home/xyz/bin
In this example, the
/home/xyz/bin
directory is added to the PATH, a useful technique if you
want to run a bunch of commands from a directory that is not normally in your PATH, without
typing the full or relative path each time.
If you decide that you no longer want a variable to be set, you can use the
unset
command to
erase its value. For example, you can type unset XYZ, which causes
XYZ
to have no value set.
(Remember to remove the export from the
$HOME/.bashrc
file -- if you added it there -- or it
will return the next time you open a shell.)
Managing Background and Foreground Processes
If you are using Linux over a network or from a dumb terminal (a monitor that allows only text
input with no GUI support), your shell may be all that you have. You may be used to a graphical
environment where you have a lot of programs active at the same time so that you can switch
among them as needed. This shell thing can seem pretty limited.
Although the bash shell doesn't include a GUI for running many programs, it does let you move
active programs between the background and foreground. In this way, you can have a lot of stuff
running, while selectively choosing the program you want to deal with at the moment.
There are several ways to place an active program in the background. One mentioned earlier is to
add an ampersand (
&
) to the end of a command line. Another way is to use the
at
command to
run commands in a way in which they are not connected to the shell.
To stop a running command and put it in the background, press Ctrl+Z. After the command is
stopped, you can either bring it back into the foreground to run (the
fg
command) or start it run-
ning in the background (the
bg
command). Keep in mind that any command running in the
63
Running Commands from the Shell
2