Bash

GNU Bash (Bourne-again shell) is Article description::the default shell on Gentoo systems and a popular [[shell program found on many Linux systems.]]

See the terminal emulator article for some general usage pointers.

Installation
is part of the @system set and comes installed on every Gentoo system. It is also used by Portage, Gentoo's default package manager. It is highly recommended to not uninstall (or any other package in the @system set), even if another shell is used as a login-shell.

USE flags
It is possible to change USE flags for the bash package:

After making USE modifications, ask Portage to update the system so the changes take effect:

Login shell
The default login shell for a user is defined in the file. The login shell can be changed using the utility, which is part of  package.

Files
There are many settings to consider when determining how to modify the shell's behavior. Configuration changes can be defined via variables, functions, or shell built-ins. These settings are defined in several different configuration files, where the settings in the last file parsed will overwrite entries in previously defined files.


 * - Initial settings for all users.
 * - Settings for this user.
 * - Settings for this user, if does not exist.
 * - Settings for this user, if and/or  do not exist.

If the shell is started without login (e.g. in a terminal on a desktop), the following files are used:


 * - Initial settings for all users.
 * - Settings on a per-user basis.

In Gentoo, and many other Linux distributions, the file is parsed in  to ensure that  and  are always checked when someone logs into the system. If it exists, the final settings are always defined by the user in each file.

Tab completion
The package adds completion to many programs and their parameters. To enable completion, just merge the package. No special use flags for packages, which support completion, are required.

Bash completion for all supported programs is enabled by default. Available completions using and specific completions disabled using

To disable bash completion on a per user basis, create an file with the following content in the user's home directory, then open a new bash shell:

Environment variables
See all variables for the current shell process which have the export attribute set:

Of course, users can export their own variables, which are available to the current process and inherited by child processes:

Environment variables can also be localized to an individual child process by prepending an assignment list to a simple command. The resulting environment passed to  will be the union of the assignment list with the environment of the calling shell process:

To check the value of a variable:

PS1
The special shell variable named PS1 defines what the terminal prompt looks like:

This prompt would be the following value for the PS1 variable:

The following table lists the possible placeholders that can be used in the PS1 variable:

Complete commands can be put into prompt using a command substitution. The following will execute the command to show the one-minute load average at the beginning of the prompt:

Looks like:

Having colors in the prompt:

The  changes the color for every next output, put   at the end of the variable to reset the color, otherwise the whole prompt will be in green.

Color codes:

The  in   means foreground. If desired, other values like  for foreground bold and   for foreground underlined can be defined. Omit this number to refer to the background, e.g..

Readers may find this PS1 generator website useful when attempting to modify the PS1 variable.

set
The set command is a shell built-in used to display and change settings in the bash shell.

Show all current settings:

Disable the shell history:

Enable the shell history:

alias
The builtin can be used to define a new command or redefine an existing command:

Now whenever (two lowercase Ls) is send to the shell, it will actually execute.

To remove an alias:

To temporarily bypass an alias escape the first letter of the command with a backslash character:

Listing
Run without any arguments to display a list of currently defined aliases:

history
The history of used commands in a session is written to a file in the user's home directory. The easiest way to access the commands in the history is using the and  keys.

To show all commands in the current history:

To search for commands in the history, by piping the output through and filter for words:

The commands are numbered and can be executed using their index:

To execute the last command used:

Delete every command in the history:

Show the current settings for history:

Keyboard shortcuts
bash includes two different keyboard shortcuts modes to make editing input on the command-line easier: emacs mode and vi mode. bash defaults to emacs mode.

vi mode
vi mode requires an key press to prefix very movement or edit, so it can be a bit awkward to learn this mode. To change the mode to vi mode, execute the following command:

Review this bash vi editing mode cheat sheet document by Peteris Krumins for more details on key bindings in vim mode.

emacs mode
To switch to emacs mode (which is the default mode):

Movement:


 * + : Move the cursor to the beginning of the line (Home).
 * + : Move the cursor to the end of the line (End).
 * + : Move the cursor forward one character.
 * + : Move the cursor back one character.
 * + : Toggle the cursor between the current position and the beginning of the line.

Screen control:


 * + : Stop (pause) output on the screen.
 * + : Resume output on the screen (after stopping it with the previous command).
 * + : Clears the screen (very similar to the command).

Scripts
Shell scripts are text files which contain programs written in a certain shell scripting language. Which shell is used to interpret the commands in a script is defined by the first line of the script. This consists of two special characters  (called the shebang), then a direct path to the shell. For example:

If no shell is defined the default shell for the user who executes the script is used. Often is used, which is the father of all shells and has very limited functionalities. Nearly all shells available understand commands used when running, so those scripts are highly portable.

Start scripts
To run scripts directly from the command-line, they need to be executable. To make a shell script executable:

Now the script can be executed by using the prefix, where either the shell defined by the shebang in the script or the default shell of the user is used:

Redirection
In Bash it is possible to redirect the output of one program into the input of another program using a pipe, indicated by the  symbol. This enables users to create command chains. Here is an example to redirect the output of into the program :

To redirect output into a file:

The  operator will erase any previous content before adding new one. If this is not desired, use the  (append) operator instead.

Logical operators
Logical operators are very useful to chain commands together. This is helpful when checking if the previous command finished successfully or not.

(AND) - The following command prints 'Success' only if our test script is successful:

(OR) - The following command prints 'Failure' only if our test script is unsuccessful:

Jobs
Usually when script or command has been executed shell input is blocked until after execution has completed. To start a program directly in the background, append the ampersand character to the end of the command:

This will execute the script as job number 1 and the prompt expects the next input.

When a program is already running, but the shell is needed for another task, it is possible to move programs from foreground to background and vice versa. To get a command prompt when a command is running on the shell, put it into sleep using +, then move it to the background:

To list all jobs running in the background:

To move a job back to foreground:

Command substitution
Using a command substitution, it is possible to run programs as parameters of other commands like here:

This will first execute the command in the brackets and append the output as parameter of emerge.

More substitutions can be performed in one command like this:

Garbled display
The output of a shell can, in some conditions, become corrupt. See the terminal emulator article for instructions to help fix this.

External resources

 * Bash reference from the Gentoo Developer's Handbook.
 * Advanced Bash-Scripting Guide - From the Linux Documentation Project.
 * Chet's Bash page
 * Official upstream documentation
 * The Bash FAQ and Bash guide on Greg Wooledge's wiki
 * bash-hackers - A wiki covering bash syntax (a very good reference for shell scripting).
 * Bash cgit
 * POSIX sh spec
 * mksh, ksh93, and ksh88 manuals for cross-reference
 * Comprehensive Beginner Linux Tutorial