The Bash Shell

Bash is short for Bourne Again Shell. This shell is available as free software under the GNU license. The name reflects the bash shell's origins as an enhanced version of the Bourne shell. It also is typical of the sly sense of humor—some might say silliness—sometimes used in naming Linux free software programs.

Bash is the default Red Hat Linux shell. It is generally an easy shell to work with. According to the Free Software Foundation, "bash is an sh-compatible shell that incorporates useful features from the Korn shell ksh and the C shell csh."

Unless you are already used to another shell, you probably should stick with the bash shell.

Working with environment variables

An environment variable is used to expose information to the shell and to programs invoked at the shell's prompt. In other words, the information contained in an environment variable is used to determine interactions among the user, the shell, the system, and programs.

By convention, environment variables are all uppercase. Also, you need to know that $ is used as a special character to mean the content of a variable rather than the literal string that is the variable's name.

It is typical for an environment variable to be used to contain a location. For example, the environment variable HOME is used to store a user's home directory.

Some environment variables are used to tell a particular program the location of files it needs. For example, to invoke the PostgreSQL's Postmaster service, the user postgres—described in Chapter 9—had to have the environment variable PGLIB set to the location of PostgreSQL's data files.

Many environment variables are set in the overall or default configuration files. (Configuration files are explained later in this chapter.) Until they are changed for a particular user, these environment variables are set the same for all users.

Other environment variables are specific to a particular user, like the postgres user I just mentioned. This occurs when the environment variable is set on the fly interactively, or added only to a user-specific configuration file.

The echo command displays a line of text.

To see the current value of an environment variable:

1.
At the command prompt, type echo followed by the environment variable. Here are some examples:

echo $HOME(user's home directory)
echo $LOGNAME(user's logon ID)
echo $SHELL(current shell)
echo $PATH(path)

2.
Press Enter.

Tip

If the environment variable you attempt to echo doesn't exist, the line echoed back will be empty.


Tip

If you leave off the $, for example, if you enter echo HOME the response will be the literal HOME.


To list all the current environment variables:

1.
At the command prompt, type set.

2.
Press Enter.

The environment variables will be displayed (Figure 10.9).

Figure 10.9. To display the environment variables, type set.


Piping to more

When you displayed your environment variables using the set command, they may have filled more than one terminal or telnet screen, so you couldn't view the variables at the beginning.

The more Linux command displays file contents one screen at a time. When you've finished viewing each screen, you press the spacebar to move on to the next screen.

To pipe a command means to send the output of one Linux command to another. The symbol that indicates piping is |.

To view environment variables one screen at a time:

1.
At the command prompt, type

									set | more
								

2.
Press Enter.

Tip

The Pipe symbol (|) is usually found above the backslash key on the keyboard. It is often represented with two dotted vertical lines rather than a solid vertical line.


"Piping to more" is very useful and can be used to view directory listings.

To view a directory listing one screen at a time:

1.
At the command prompt, type

									ls –a –l | more
								

2.
Press Enter.

The directory listing will appear, one screen at a time (Figure 10.10).

Figure 10.10. To view a directory listing one screen at a time, use the Ls command piped to the more command.


The more command can also be used to read the contents of files.

To use more to view the contents of a file:

1.
At the command prompt, type more followed by the file name: for example, more .bash_profile.

2.
Press Enter.

The contents of the file will be displayed one screen at a time.

Setting environment variables

You can set environment variables interactively at the command line. This is true for environment variables that you make up on the spot, those that programs require, and those that come preconfigured—although you should probably be a bit circumspect about editing variables that the shell sets automatically.

Keep in mind that an environment variable set on the fly exists only as long as the session in which it was created. This is sometimes useful in debugging configuration issues, but if you want the environment variable to have its value the next time you log on, you will need to set it in a configuration file. I'll explain how to do this later in this chapter.

Setting an environment variable involves two steps:

  • Assigning the variable

  • Exporting it so that it is available to all programs and scripts running in the current session

To set an environment variable on the fly:

1.
At the command prompt, type the name of the environment variable followed by the value you want assigned to it.

This value is often a file name or location, but it can also be a text string or number: for example, MYENV ah-goo! assigns the text string ah-goo! to the environment variable MYENV.

2.
Press Enter.

3.
At the prompt, export the environment variable: for example, export MYENV.

4.
Press Enter.

5.
Verify that the value has been assigned by typing echo $MYENV at the prompt.

The value of the variable will be displayed on the next line (Figure 10.11).

Figure 10.11. Environment variables must be exported before they are available in scripts and programs.


Tip

The two steps—assigning and exporting—can be combined into one: for example, export NEXTENV=42.


Setting the PATH environment variable

A very important environment variable, PATH is used to store the value of the path—the directories where the shell can look for files that have been invoked without a fully qualified path being required.

For example, if myprogram is in the /harold/ bin directory, I can run myprogram simply by typing myprogram if /harold/bin is on the path—that is, if /harold/bin is included in the PATH environment variable.

On the other hand, if /harold/bin is not on the path, to invoke myprogram I would have to include its location:

							/harold/bin/myprogram
						

Within the PATH variable, the various directories that are on the path—that is, that are available without full qualification—are separated by colons (:), like this:

							/bin:/usr/bin:/usr/X11R6/bin:/harold:/harold/bin
						

If you were adding a new directory to your path, one thing you would not want to do would be to delete the current contents of the PATH variable. If you did so, a number of things quite possibly wouldn't work right.

Fortunately, the $ operator can be used to reference the contents of the current PATH variable. New additions to the PATH variable can then be concatenated with the existing value.

To add a directory to the PATH variable:

  • At the command prompt, type

    									export PATH=$PATH:
    									new directory
    								

    For example, type

    									export PATH=$PATH:/harold/bin
    								

    Tip

    If the PATH string contains any spaces, you should use quotation marks. For example,

    										export PATH= "$PATH:/harold/my wpdocs"
    									

Editing the configuration files

Editing or adding environment variables at the command prompt is all very well and good, but your changes do not persist, meaning that the next time you log on, they will be gone.

If you want to create environment variables (or change existing ones) and have your work persist between sessions, you need to edit a configuration file.

If you are using Red Hat Linux 6 and the bash shell, there are four relevant configuration files, listed in Table 10.2.

When you look at Table 10.2, you'll see a special character being used: the tilde (~). This character indicates the user's home directory.

To go to your home directory:

1.
At the command prompt, type the following:

									cd ~
								

2.
Press Enter.

If you look at Table 10.2, you'll see that the files in the user's home directory start with a period: for example, .bash_profile. The period means that they are hidden files.

If you open the Gnome File Manager with its default settings, hidden files will not be displayed.

Typical Red Hat Linux 6 ash Configuration Files
Location and FileProcessing orderContents
/etc/profileFirstSystem-wide environment variables and startup programs
/etc/bashrcSecondIn theory, system-wide functions and aliases; in practice often only the prompt configuration
~/.bash_profile (in user's home directory)If it exits, thirdUser-specific environment variables and startup programs
~/.bashrc (in user's home directory)If it exists, last; this fileUser-specific functions and aliases is invoked by .bash_profile

To view hidden files in Gnome File Manager:
1.
Open Gnome File Manager.

2.
Choose Preferences from the Edit menu.

The File display tab of the Preferences dialog box will open (Figure 10.12).

Figure 10.12. To view hidden files in Gnome file Manager, check Show Hidden Files in the Preferences dialog box.


3.
Select Show Hidden Files to place a check mark next to it.

4.
Click OK.

Tip

For information on how to display hidden files using the command line, see Chapter 11.


From a system administrator's viewpoint, if you create a user with the Gnome/Linux Configuration tool (see Figure 10.13), and if the home directory you set is under the /home directory, then the configuration files stored in /etc/skel are automatically copied to the user's new home directory.

Figure 10.13. Users assigned a home directory below /home have the template configuration files located in etc/skel automatically copied to their home directories.


In this case, skel is shorthand for skeleton, meaning a template. As an administrator, if you want to change all of the configuration files for new users, you would simply edit the files in etc/skel.

Of course, it would be the administrator's option to manually tweak a specific user's configuration files—or to add a user with no configuration files at all, for that matter.

For more information on using the Gnome/ Linux Configuration tool to add users and groups, see Chapter 6. For information on administering users at the command line, see Chapter 11.

To add an environment variable forauser:

1.
On the GnomeLinux desktop, open Gnome File Manager.

2.
Find the .bash_profile file located in the user's home directory (Figure 10.14).

Figure 10.14. You can use Gnome File Manager to locate a user's bash-profile file.


3.
Right-click bash-profile and choose Open With from the fly-out menu.

The gmc dialog box will open (Figure 10.15).

Figure 10.15. To edit a file in Gnotepad+, type gnp in the Program to Run box


4.
Type gnp in the Program to Run box.

5.
Click OK.

The .bash_profile file will open in Gnotepad+ (Figure 10.16).

Figure 10.16. It's easy to edit a text file, such as a user's bash-profile configuration file, in Gnotepad+.


6.
Using the techniques described earlier in this chapter in "To set an environment variable on the fly," add and export an environment variable: for example,

										MYENV= "Lions, and tigers, _
   and bears,  Oh my!"
export MYENV

7.
Choose Save from the Gnotepad+ File menu and close Gnotepad+.

8.
Log on as the user whose configuration file was changed.

9.
At the prompt, type echo $MYENV.

The new environment variable will be displayed (Figure 10.17).

Figure 10.17. Changes made in a user's configuration file are available the next time the user logs on.


Tip

You can edit configuration files using the text editors available at the command line rather than Gnotepad+. Some of the more popular command-line text editing tools are described later in this chapter.


Tip

To add an environment variable globally rather than for a single user, edit the etc/profile file rather than the user's .bash_profile file.


To add to the path globally

1.
Use the techniques described in "To add an environment variable for a user" to open the file etc/profile in Gnotepad+ (Figure 10.18).

Figure 10.18. By editing the etc/profile, you can make changes to everyone's path.


2.
Using the techniques described in "To add a directory to the PATH variable," add a directory to the path. For example, enter:

										PATH="$PATH:/home/hdavis/ _
   Linuxvqs"

3.
Make sure the PATH variable is exported.

4.
Save the file.

5.
To verify the addition, log on.

6.
At the prompt, type echo $PATH.

7.
Press Enter.

The new directory will be displayed on the path (Figure 10.19).

Figure 10.19. Changes made to the path will be available the next time you log on.


Tip

If you delete any directories from the path without knowing what they do, it is quite possible that some things will not work any more.


Tip

To change a user's path, rather than the global path, edit that user's .bash_profile file rather than etc/profile.


Changing the bash prompt

You can easily configure the command-line prompt to include various useful pieces of information. To change the system-wide prompt, edit /etc/bashrc. To change a single user prompt, edit the .bashrc file in that user's home directory.

In either case, you will need to edit the line beginning PS1, which is often the only line that is not a comment in these files. The various kinds of information that you can include go between the square brackets and are represented by code letters (see Table 10.3). You can also include literal text if you wish.

To add full directory, date, and time information to the prompt:

1.
Open /etc/bashrc in a text editor (Gnotepad+ is shown in Figure 10.20).

Figure 10.20. To change the appearance of the prompt, edit the PS1 line in/etc/bashrc


2.
Place a hash symbol (#) in front of the current prompt line (it begins with PS1).

This will comment out the line, disabling it but leaving it in place in case you want to go back to it.

3.
Add a new PS1 line. For example, enter:

									PS1="[u working in w Date: _
   d Time:   	]\$"
								

4.
Save the file.

5.
To verify the results, log on.

The new prompt should appear (Figure 10.21).

Figure 10.21. You can include various kinds of information in the prompt.


Tip

Consider testing a proposed prompt interactively to see how you like it by changing the PS1 environment variable before making a global change.


Prompt Codes
CodeMeaning
uDisplays the user ID of the current user
wShows the current path and directory; the user's home directory is represented with a ~ character
WShows the current directory without the path
Shows the current time
dShows the current date
Forces a new line within the prompt, making the prompt split between two lines
hShows the host name of the Linux server

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.40.189