The bash environment
From Linux 101, The beginner's guide to all things Linux.
This is the second article on the bash shell.
Contents |
[edit] Command History
bash stores the commands you type in two places: memory and in your history file. This means even when you start bash all of your old commands can be available to you. bash will only save to the history file when you close out the shell. Also each running copy of bash has its own command memory. So if you open one shell, type a command, and then open another and hit the up arrow, you won't see the command you just typed in the first one.
Use the up and down arrows to scroll through commands you have typed in. You can also use the left and right arrows to go into your command and edit something (perhaps a typo) or change an option, etc.
Another useful tip is to use the command history operator, the !. If you begin a line with it, then at least one letter, bash will look up (from most recent to oldest) the command history for the command that matches. When it finds it, it will execute it again, saving you from typing. Some examples:
ssh shells.sf.net
... later on after other commands ...
!ssh
will run ssh shells.sf.net again. You could do fewer letters than !ssh, such as !ss or !s if you haven't run any other commands starting with ss or s, respectively.
[edit] Built-in Commands
Although bash is used to start programs, some commands bash handles internally. These are used to configure bash and do some other cool things, which we will discuss later. If you are ever curious if a program is built in or an actual program, the type program will tell you. Simply use it as:
type <command name>
[edit] Environmental Variables
Previously discussed in Basic Commands, environmental variables are a part of the bash shell. To see a listing of them, type:
set
and observe that you have a long listing. As you may have guessed, set is a built-in command. The import ones are as follows:
- BASH
- the full path to the bash executable
- CC
- the C compiler to use when making a program
- COLUMNS
- how many columns are in the current shell window (default 80)
- CXX
- the C++ compiler to use when making a program
- DISPLAY
- the X display to use for GUI programs started at the shell
- EDITOR
- the default system text editor
- HISTFILE
- the file command history is stored in
- HISTFILESIZE
- maximum file command history file size
- HISTSIZE
- maximum command history stored in memory
- HOME
- the user's home directory
- HOSTNAME
- the system's hostname
- HOSTTYPE
- the system's host type
- LINES
- how many rows are in the current shell window (default 25)
- MANPATH
- the directories the man pages can be found in
- PAGER
- the program used when there is lengthy output
- PATH
- lists the directories executables can be found in
- PS1
PS2
PS3
PS4 - described below
- UID
- the user ID of the user running the shell
- USER
USERNAME - the username of the user running the shell
[edit] Assigning Values to Environmental Variables
Making a new variable or reassigning a value to an existing one is very simple. All you have to do is type:
VARIABLE=VALUE
at the prompt. Make sure you do not use any spaces. If you use spaces the bash interpreter will think VARIABLE is a program you are trying to run. If the VALUE you are trying to set has spaces in it, you need to enclose the VALUE in quotation marks. Some examples follow:
CC=gccPS1="\u@\h \W"
[edit] Temporary Values
Sometimes you will want to just assign a temporary value, run a program, and then have it return to the original value. This is easily accomplished.
VARIABLE=VALUE program <arguments>
A good example of this is on a gentoo system for temporary USE flags.
USE="gd png" emerge php
and after emerge executes, USE returns to its previous value.
[edit] Using Variables
We have seen a previous example of using variables. It was:
echo $PATH
From this you might gather that putting the dollar sign in front of the variable name is how to use them, and you would be correct. It is very likely that you will only really begin to use environmental variables once you start getting into scripting, which we are going to cover in a later article.
[edit] The PS variables
The Prompt Statement variables (PS1 to PS4) are what bash uses to indicate a prompt is available, and you need to type something in.
- PS1 - the standard ready to receive a command
- PS2 - more input is expected to complete a command
- PS3 and PS4 are used less often, and are nothing to be concerned with at this time.
The Prompt statements allow for special backslash-escaped characters to be used to substitute in useful information. A complete listing is available in the man page, but the popular ones are:
- \h - the sytem hostname up to the first period
- \H - the complete system hostname
- \u - the username of the current user
- \w - the complete path of the current working directory (ie, /home/username/downloads/)
- \W - the basename of the current working directory (ie, downloads)
This information is useful for customizing the command prompt display for your user. Between my main user and my root user, I use different PS1 styles such that I am aware of whether or not I have su'ed to root.
[edit] Saving bash Settings
Environmental variables are stored indepently for each process of bash. That is, if you have 3 shell windows open in your GUI environment and you set a variable in one, the other 2 have no idea you changed the variable value. bash also does not automatically save the variables you had when you close a window.
If you want bash to remember values, then you should edit your ~/.bashrc file. This file is executed each time you start bash, so if you were to edit it, any changes you make would become available to all future processes of bash.
|
The Bash Articles Series |
|---|
| The shells and specifically bash | The bash environment | Processes and signals | Simple shell scripts | Shell scripting |

