Bash (Bourne-again Shell) is a command-line shell/programming language by the GNU Project. Its name is a homaging reference to its predecessor: the long-deprecated Bourne shell. Bash can be run on most UNIX-like operating systems, including GNU/Linux.
- 1 Invocation
- 2 Command line
- 3 Aliases
- 4 Tips and tricks
- 5 Troubleshooting
- 6 See also
- 7 Acknowledgement
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 -l/--login command line option.
Bash is considered an interactive shell when its standard input and error are connected to a terminal (for example, when run in a terminal emulator), and it is not started with the -c option or non-option arguments (for example, bash script). All interactive shells source /etc/bash.bashrc and ~/.bashrc, while interactive login shells also source /etc/profile and ~/.bash_profile.
1.1 Configuration files
|File||Description||Login shells (see note)||Interactive, non-login shells|
|/etc/profile||Sources application settings in /etc/profile.d/*.sh and /etc/bash.bashrc.||Template:Yes||Template:No|
|~/.bash_profile||Per-user, after /etc/profile. If this file does not exist, ~/.bash_login and ~/.profile are checked in that order. The skeleton file /etc/skel/.bash_profile also sources ~/.bashrc.||Template:Yes||Template:No|
|~/.bash_logout||After exit of a login shell.||Template:Yes||Template:No|
|/etc/bash.bashrc||Depends on the -DSYS_BASHRC="/etc/bash.bashrc" compilation flag. Sources /usr/share/bash-completion/bash_completion.||Template:No||Template:Yes|
|~/.bashrc||Per-user, after /etc/bash.bashrc.||Template:No||Template:Yes|
1.2 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
Environment variables are conventionally placed in ~/.profile or /etc/profile so that all bourne-compatible shells can use them.
See Environment variables for more general information.
2 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.
2.1 Tab completion
Tab completion is the option to auto-complete typed commands by pressing Tab (enabled by default).
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.
2.1.2 Common programs and options
By default, Bash only tab-completes commands, filenames, and variables. The package bash-completion extends this by adding more specialized tab completions for common commands and their options. With bash-completion, normal completions (such as $ ls file.*<tab><tab>) will behave differently; however, they can be re-enabled with $ compopt -o bashdefault program (see  and  for more detail).
2.1.3 Customize per-command
By default Bash only tab-completes file names following a command. You can change it to complete command names using complete -c:
complete -c man which
or complete command names and file names with -cf:
complete -cf sudo
See the Bash man page for more completion options.
2.2.1 History completion
bind '"\e[A": history-search-backward' bind '"\e[B": history-search-forward'
or to affect all readline programs:
"\e[A": history-search-backward "\e[B": history-search-forward
2.2.2 Shorter history
The HISTCONTROL variable can prevent certain commands from being logged to the history. For example, to stop logging of repeated identical commands
or set it to erasedups 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.
2.3 Mimic Zsh run-help ability
Zsh can invoke the manual for the command preceding the cursor by pressing Alt+h. A similar behaviour is obtained in Bash using this Readline bind:
bind '"\eh": "\C-a\eb\ed\C-y\e#man \C-y\C-m\C-p\C-p\C-a\C-d\C-e"'
This assumes are you using the (default) Emacs editing mode.
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 are preferably stored in ~/.bashrc, and system-wide aliases (which affect all users) belong in /etc/bash.bashrc. See  for example aliases.
For functions, see Bash/Functions.
4 Tips and tricks
4.1 Prompt customization
4.2 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 may be found in the following packages: extra/abiword 3.0.1-2 /usr/bin/abiword
4.3 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 nano
4.4 Clear the screen after logging out
To clear the screen after logging out on a virtual terminal:
4.5 Auto "cd" when entering just a path
Bash can automatically prepend cd when entering just a path in the shell. For example:
bash: /etc: Is a directory
But after adding one line into .bashrc file:
... shopt -s autocd ...
[user@host ~]$ /etc cd /etc [user@host etc]$
autojump allows navigating the file system by searching for strings in a database with the user's most-visited paths.
After installation, /etc/profile.d/autojump.bash must be sourced in order to start using the application.
5.1 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 checkwinsize shell option checks the window size after each command and, if necessary, updates the values of LINES and COLUMNS.
shopt -s checkwinsize
5.2 Shell exits even if ignoreeof set
If you have set the ignoreeof option and you find that repeatedly hitting ctrl-d 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.
6 See also
- Bash Reference Manual, or /usr/share/doc/bash/bashref.html
- Readline Init File Syntax
- The Bourne-Again Shell - The third chapter of The Architecture of Open Source Applications
- Shellcheck - Check bash scripts for common errors
- BashGuide on Greg's Wiki
- BashFAQ on Greg's Wiki
- Bash Hackers Wiki
- Bash Hackers: List of Bash online-tutorials
- Quote Tutorial