Bash

Bash (Bourne-again Shell) is a command-line shell/programming language by the GNU Project. Its name alludes to its predecessor, the long-deprecated Bourne shell. Bash can be run on most UNIX-like operating systems, including GNU/Linux.

Bash is the default command-line shell on Arch Linux.

Invocation

Bash behaviour can be altered depending on how it is invoked. Some descriptions of different modes follow.

If Bash is spawned by login in a TTY, by an SSH daemon, or similar means, it is considered a login shell. This mode can also be engaged using the /--login command line option.

Bash is considered an interactive shell when its standard input, output and error are connected to a terminal (for example, when run in a terminal emulator), and it is not started with the option or non-option arguments (for example, ). All interactive shells source and ~/.bashrc, while interactive login shells also source and .

Configuration files

Bash will attempt to execute a set of startup files depending on how it was invoked. See the Bash Startup Files section of the GNU Bash manual for a complete description.

File Description Login shells (see note) Interactive, non-login shells
Sources application settings in /etc/profile.d/*.sh and .
Per-user, after . If this file does not exist, and are checked in that order. The skeleton file also sources ~/.bashrc.
~/.bash_logout Per-user, after exit of a login shell.
Depends on the compilation flag. After exit of a login shell.
Depends on the compilation flag. Sources /usr/share/bash-completion/bash_completion.
~/.bashrc Per-user, after .

Shell and environment variables

The behavior of Bash and programs run by it can be influenced by a number of environment variables. Environment variables are used to store useful values such as command search directories, or which browser to use. When a new shell or script is launched it inherits its parent's variables, thus starting with an internal set of shell variables.

These shell variables in Bash can be exported in order to become environment variables:

VARIABLE=content
export VARIABLE

or with a shortcut

export VARIABLE=content

Environment variables are conventionally placed in or so that other Bourne-compatible shells can use them.

See Environment variables for more general information.

Command line

Bash command line is managed by the separate library called Readline. Readline provides emacs and vi styles of shortcuts for interacting with the command line, i.e. moving back and forth on the word basis, deleting words etc. It is also Readline's responsibility to manage history of input commands. Last, but not least, it allows you to create macros.

Tab completion

Tab completion is the option to auto-complete typed commands by pressing (enabled by default).

Single-tab

It may require up to three tab-presses to show all possible completions for a command. To reduce the needed number of tab-presses, see Readline#Faster completion.

Common programs and options

By default, Bash only tab-completes commands, filenames, and variables. The package extends this by adding more specialized tab completions for common commands and their options, which can be enabled by sourcing /usr/share/bash-completion/bash_completion (which has been already sourced in Arch's ). With , normal completions (such as ) will behave differently; however, they can be re-enabled with (see and for more detail).

Customize per-command

Note: Using the complete builtin may cause conflicts with bash-completion.

By default Bash only tab-completes file names following a command. You can change it to complete command names using : or complete command names and file names with :

complete -cf sudo

See the Bash man page for more completion options.

History completion

You can bind the up and down arrow keys to search through Bash's history (see: Readline#History and Readline Init File Syntax):

or to affect all readline programs:

Shorter history

The variable can prevent certain commands from being logged to the history. For example, to stop logging of repeated identical commands or set it to to ensure that Bash's history will only contain one copy of each command (regardless of order). See the Bash man page for more options.

Disable history

To disable the bash history only temporarily:

$ set +o history

The commands entered now are not logged to the .

For example, now you can hash passwords with , or hide GPG usage like and your secret is not written to disk.

To enable history:

$ set -o history
Tip: If the HISTCONTROL variable contains ignorespace, commands starting with a space will not be saved into the history file. See bash(1) §Shell Variables for details.

To disable all bash history:

... and just to make sure, destroy your old histfile forever:

$ wipe -i -l2 -x4 -p4 "$HISTFILE"
$ ln -sv /dev/null "$HISTFILE"

Mimic Zsh run-help ability

Zsh can invoke the manual for the command preceding the cursor by pressing . A similar behaviour is obtained in Bash using this Readline bind:

This assumes are you using the (default) Emacs editing mode.

Aliases

alias is a command, which enables a replacement of a word with another string. It is often used for abbreviating a system command, or for adding default arguments to a regularly used command.

Personal aliases can be stored in ~/.bashrc or any separate file sourced from ~/.bashrc. System-wide aliases (which affect all users) belong in . See for example aliases.

For functions, see Bash/Functions.

Tips and tricks

Prompt customization

See Bash/Prompt customization.

Syntax highlighting and autosuggestions

is a command line editor written in pure Bash which replaces Readline. It has many enhanced features like syntax highlighting, autosuggestions, menu-completion, abbreviations, Vim editing mode, hook functions, and more.

After installing it, source it in an interactive session. Configurations are explained in depth in the ~/.blerc file and at the wiki. The stable package is also available.

Command not found

pkgfile includes a "command not found" hook that will automatically search the official repositories, when entering an unrecognized command.

You need to source the hook to enable it, for example:

Then attempting to run an unavailable command will show the following info:

$ abiword
abiword may be found in the following packages:
  extra/abiword 3.0.1-2	/usr/bin/abiword

An alternative "command not found" hook is provided by , which looks like this:

Disable Ctrl+z in terminal

You can disable the Ctrl+z feature (pauses/closes your application) by wrapping your command like this:

#!/bin/bash
trap "" 20
adom

Now when you accidentally press Ctrl+z in instead of nothing will happen because Ctrl+z will be ignored.

Clear the screen after logging out

To clear the screen after logging out on a virtual terminal:

Auto "cd" when entering just a path

Bash can automatically prepend when entering just a path in the shell. For example:

But after adding one line into file:

You get:

[user@host ~]$ /etc
cd /etc
[user@host etc]$

Autojump

autojump-gitAUR allows navigating the file system by searching for strings in a database with the user's most-visited paths. After installation, must be sourced in order to start using the application.

An alternative is , which has additional features and performance improvements compared to the original .

Prevent overwrite of files

For the current session, to disallow existing regular files to be overwritten by redirection of shell output:

$ set -o noclobber

This is identical to .

To make the changes persistent for your user:

To manually overwrite a file while noclobber is set:

$ echo "output" >| file.txt

Troubleshooting

Line wrap on window resize

When resizing a terminal emulator, Bash may not receive the resize signal. This will cause typed text to not wrap correctly and overlap the prompt. The shell option checks the window size after each command and, if necessary, updates the values of and .

~/.bashrc
shopt -s checkwinsize

Shell exits even if ignoreeof set

If you have set the option and you find that repeatedly hitting causes the shell to exit, it is because this option only allows 10 consecutive invocations of this keybinding (or 10 consecutive EOF characters, to be precise), before exiting the shell.

To allow higher values, you have to use the IGNOREEOF variable.

For example:

export IGNOREEOF=100

Checking errors by analyzing scripts

The package analyzes bash (and other shell) scripts, prints possible errors, and suggests better coding.

There is also the web site shellcheck.net of the same purpose, based on this program.

See also

Tutorials

Community

Examples

This article is issued from Archlinux. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.