Bash

GNU Bash (Bourne-again shell) is a shell program. It is the first program started if someone logs in at a terminal. Which user uses what shell is defined in the file. It enables users to easier interact with the system and start additional programs. A lot of this information also applies to other shells like zsh.

Installation
is part of the system set and so already installed on your system. It is also used by portage, Gentoo's default package manager, so it is not recommended to uninstall it, even if you use another shell as login-shell.

But you can change the USE flags:

After setting this you want to update your system so the changes take effect:

Shell
The default shell for a user is defined in. It can be changed using chsh, which is part of.

Configs
Many settings on how the shell behaves, can be defined via variables. Those variables are defined in several different configuration files, where the settings in the last file parsed do overwrite previous definitions.

If the shell is started without login (e.g. in a terminal on a desktop), the following files are used In Gentoo and many other distributions is parsed in the  to ensure that  and  are always checked when someone logs into the system. The final settings are defined by the user in their
 * - initial settings for all users
 * - settings for this user
 * - settings for this user, if doesn't exist
 * - settings for this user, if and  don't exist
 * - initial settings for all users
 * - settings for this user

Tab completion
bash-completion adds completion to many programs and their parameters.

You need to add the following line to your to load bash-completion.

(The script is located at in .)

Now you can enable completion for various programs with eselect.

You may want to enable completion for all currently installed packages for the current user at once, example with root:

To enable completion globally for all currently installed packages:

You will get an error message /usr/share/bash-completion/completions doesn't exist, and you can safely ignore it.

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 execve 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 PS1 defines what the prompt looks like.

This prompt would be the following value in PS1:

The following table lists the possible placeholders you can use in your PS1 variable: You can also put complete commands into your prompt using a command substitution. Here we want to execute cut -d\ -f1 /proc/loadavg to show the one-minute load average at the beginning of the prompt:

which looks like this:

Having colours in the prompt:

The \e[0;32m\] changes the colour for every next output, we have to put \e[0m\] at the end of our variable to reset the colour, or we would type everything in green.

Colour codes: The 0; in \e[0;31m\] means foreground. You can define other values like 1; for foreground bold and 4; for foreground underlined. Omit this number to refer to the background, e.g. \e[31m\].

set
Display and change settings in the Bash shell.
 * Show all current settings:
 * Disable the shell history:
 * Enable the shell history:

alias
You can use the  builtin to define a new command or redefine an existing command:

Whenever now ll is send to the shell, it will actually execute ls -l.

To remove an alias:

If you want to temporarily bypass an alias you can escape the first letter of the command with a backslash character:

history
The history of used commands in a session is written to a file in the user 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 grep 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:

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 in the first line (which is called the shebang):

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

Start Scripts
To start scripts, they need to be executable. To make a shell script executable:

Now it 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:

In alternative you can explicitly invoke the shell and pass the script filename as an argument (no change of permissions needed):

The file extension .sh does not matter, but it helps to distinguish scripts from normal text files.

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 ls -l into the program /usr/bin/less:

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
Very useful to chain commands are logical operators, to check 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 if we start a script or command, the input is blocked until the command is finished. To start a program directly in the background, so we can continue to work in the shell:

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

If a program is already running and you need to do something on the shell, it is possible to move programs from foreground to background and vice versa. To get a command prompt if 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.

You can perform more substitutions in one command like this:

External resources

 * Advanced Bash-Scripting Guide
 * Chet's Bash page
 * Official Bash documentation
 * The Bash FAQ and Bash guide on Greg Wooledge's wiki
 * bash-hackers wiki (very good reference)
 * Bash cgit
 * POSIX sh spec
 * mksh, ksh93, and ksh88 manuals for cross-reference
 * Comprehensive Beginner Linux Tutorial