Bash

From Gentoo Wiki
Jump to: navigation, search
External resources

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 /etc/passwd. 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.

Contents

Installation

app-shells/bash 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:

→ Information about USE flags
USE flag Default Recommended Description
afs No Adds OpenAFS support (distributed file system)
bashlogger No Log ALL commands typed into bash; should ONLY be used in restricted environments such as honeypots
examples No Install examples, usually source code
mem-scramble No Build with custom malloc/free overwriting allocated/freed memory
net Yes Yes Enable /dev/tcp/host/port redirection
nls Yes Adds Native Language Support (using gettext - GNU locale utilities)
plugins No Add support for loading builtins at runtime via 'enable'
readline Yes Yes Enables support for libreadline, a GNU line-editing library that almost everyone wants
vanilla No No Do not add extra patches which change default behaviour; DO NOT USE THIS ON A GLOBAL SCALE as the severity of the meaning changes drastically

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

root # emerge --ask --changed-use --deep @world

Configuration

Shell

The default shell for a user is defined in /etc/passwd. It can be changed using chsh, which is part of sys-apps/coreutils.

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.

  • /etc/profile - initial settings for all users
  • /home/USER/.bash_profile - settings for this user
  • /home/USER/.bash_login - settings for this user, if /home/USER/.bash_profile doesn't exist
  • /home/USER/.profile - settings for this user, if /home/USER/.bash_profile and /home/USER/.bash_login don't exist

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

  • /etc/bashrc - initial settings for all users
  • /home/USER/.bashrc - settings for this user

In Gentoo any many other distributions /etc/bashrc is parsed in the /etc/profile to ensure that /etc/bashrc and /home/USER/.bashrc are always checked when someone logs into the system. The final settings are defined by the user in his .bashrc

.bashrc

Example of a /home/USER/.bashrc

File/home/USER/.bashrc

# configure PS1 command prompt
export PS1="\u@\h \w \$ "

# no double entries in the shell history
export HISTCONTROL="$HISTCONTROL erasedups:ignoreboth"

# do not overwrite files when redirecting output
set -o noclobber

# ask before performing the following command
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

Usage

Environment Variables

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

user $ printenv

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

user $ export MYSTUFF='Hello'

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:

user $ USE="kde" emerge -pv libreoffice

To check the value of a variable:

user $ echo $MYSTUFF
Note
To have variables set in every session, the file /home/USER/.bashrc should be used.

PS1

The environment variable PS1 defines how the prompt looks like. The prompt is everything displayed in from of the blinking cursor in the console:

CodePrompt

MyUserName@MyPC: ~ $

This prompt would be the following value in PS1:

CodePS1

PS1="\u@\h \w $"
Note
the ~ symbol represents the home directory /home/USER/

The following table lists the possible placeholders you can use in your PS1 variable:

Code Effect
\u Username
\h Hostname
\w Current directory
\d Current date
\t Current time
\$ Indicate the root user with '#' and normal users with '$'
\j Number of currently running tasks (jobs)

You can also put complete commands into your prompt using a subshell. Here we want to execute uname -s to show system information in the prompt:

CodePS1

PS1="\$(uname -s) $"

Having colours in the prompt:

CodePS1

PS1="\e[0;32m\]\u@\h \w >\e[0m\]"

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:

Code Colour
\e[0;30m\] Black
\e[0;37m\] White
\e[0;31m\] Red
\e[0;32m\] Green
\e[0;33m\] Yellow
\e[0;34m\] Blue
\e[0m\] Reset to standard colours

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

  • user $ set -o
    - Show all current settings
  • user $ set +o history
    - disable the shell history
  • user $ set -o history
    - enable the shell history

alias

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

user $ alias ll='ls -l'

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

To remove an alias:

user $ unalias ll
Note
No harm is done to the actual command being redefined.

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

user $ \ls

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 Up and Down keys. To show all commands in the current history:

user $ history

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

user $ history | grep echo

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

user $ !2

To execute the last command used:

user $ !!

Delete every command in the history:

user $ history -c

Show the current settings for history:

user $ echo $HISTCONTROL

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):

FileMyScript.sh

#!/bin/bash
echo 'Hello World!'

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.

Note
On many distributions /bin/sh is a symbolic link to /bin/bash.

Start Scripts

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

user $ chmod +x MyScript.sh

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:

user $ ./MyScript.sh

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

user $ sh MyScript.sh

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:

user $ ls -l | less

To redirect output into a file:

user $ ls -l > ls_l.txt

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:
user $ ./MyScript.sh && echo 'Success'
  • || (OR) - The following command prints 'Failure' only if our test script is unsuccessful:
user $ ./MyScript.sh || echo 'Failure'

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:

user $ sh MyScript.sh &

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 Ctrl+Z, then move it to the background:

user $ bg %1

To list all jobs running in the background:

user $ jobs

To move a job back to foreground:

user $ fg %1
Note
Programs running as jobs usually do not terminate once they finish execution, there will be a message if a job finished and bringing it to foreground will then terminate the program.

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

user $ emerge $(qlist -C -I x11-drivers)

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

Note
This command is quiet useful in Gentoo to quickly rebuild all X11 drivers.

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

user $ emerge $(qlist -C -I x11-drivers) $(qlist -C -I modules)

See also

Bash is the default shell for Gentoo Linux, and the language upon which its package manager specification is built.

External resources

Personal tools
Namespaces

Variants
Actions
Gentoo Websites logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Navigation
Toolbox
Categories