Bash

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 /etc/passwd. It enables users to easier interact with the system and start additional programs. Most of this information also applies to other shells like zsh

Installation
Bash is part of any stage tarball and the core system in Gentoo, it does usually not need to be installed.

Program
To install :

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, dan 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 any 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 his
 * - 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

.bashrc
Example of a

Environment Variables
See all defined variables for this user (not all are shell related):

Of course can user define their own variables, which are readily available in memory until the end of the session (terminal closes):

In the previous command we used export to have the variable for the whole session, if this is not needed you can just define the variable and the information will be released when the command finished execution:

To check the value of a variable:

PS1
The enrivonment variable PS1 defines how the prompt looks like. The prompt is everything displayed in from of the blinking cursor in the console: Prompt This prompt would be the following value in PS1: PS1

Table with available values to set in your PS1 You can also put complete commands into your prompt using a subshell. Here we want to execute uanme -s to show system information in the prompt: PS1 Having colours in the prompt: PS1 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. ColourCodes: The 0; in \e[0;31m\] means foreground, you can define other values like 1; is foreground bold, 4; is foreground underlined and without means 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
Define a new command or another command with custom options or long command lines

Whenever now ll is send to the shell, it will actual execute ls -l To remove an alias:

history
The history of used commands in a session is written to a file in the user home share. 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 its index:

To execute the last command used:

Delete every command in the history:

Show the current settings for history:

Scripts
Scripts are textfiles which contain a series of commands or complete program like definitions. Which shell is used to interpret the commands in a script is defined in the first line like this:

#!/bin/bash defines the shell to use, 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 in /bin/sh, those scripts are highly portable.

Start Scripts
To start scripts, they need to be executeable or used as a parameter for a shell. To make a shell script executeable:

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

The other way is to directly start the shell with it (no change of permissions needed):

The file extension .sh does not matter, but it helps to distinguish scripts from normal text files. In case the extension is missing, the tool /bin/file can be used to learn about its content:

It should recongnize if it is a shell, python, perl script or a binary file.

Redirection
In Bash it is possible to directly redirect output of one program into input of another program using a pipe, indicated by the | symbol. This enables user to create command chains. Here is an example to redirect the output of ls -l into the program /usr/bin/less (Pager):

To redirect output into a file:

The single > will create a new file and write the output to it, while >> would only create a new file if it doesn't exist, otherwise append the output to the existing file

Conditional Tests
Very useful to chain commands are conditional statements, to check if the previous command finished successfully or not - Here we first list all files called 'MyScript.sh' and only if this is successful, the script is started - Here we first try to run the script and if not successfullt, it gives an error message to the terminal
 * && - AND - the following command is only executed if the first was successful
 * || - OR - the following command is only executed if the first command is not successfully

Jobs and Subshell
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 my prompt expects the next input. If a program is already running and I need to do something on the shell, it is possible to move programs from foreground to background and vise 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:

Using a sub shell, it is possible to run programs as parameters of other commands like here:

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

You can run more subshells parallel at a time like this:

External resources

 * GNU Bash
 * 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.