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
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 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
~/.bashrc, while interactive login shells also source
|File||Description||Login shells (see note)||Interactive, non-login shells|
||Sources application settings in
||After exit of a login shell.||Yes||No|
||Depends on the
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
/etc/profile so that all bourne-compatible shells can use them.
See Environment variables for more general information.
Bash command line is managed by the separate library called Readline. Readline provides a lot 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 is the option to auto-complete partial typed commands by pressing
Tab twice (enabled by default).
For single press
Tab results for when a partial or no completion is possible:
set show-all-if-ambiguous on
Alternatively, for results when no completion is possible:
set show-all-if-unmodified on
Additional programs and options
Bash has native support for tab completion of: commands, filenames, and variables. This functionality can be extended with the package bash-completion; it extends its functionality by adding a subset of tab completions to popular commands and their options. With bash-completion know that normal completions (such as
$ ls file.*<tab><tab>) will behave different; however, they can be re-enabled with
$ compopt -o bashdefault <prog> (see  and  for more detail). Also for older systems bash-completion may not be resourcefully convenient.
Additional programs and options manually
For basic completion use lines in the form of
complete -cf your_command (these will conflict with the bash-completion settings):
complete -cf sudo complete -cf man
bind '"\e[A": history-search-backward' bind '"\e[B": history-search-forward'
"\e[A": history-search-backward "\e[B": history-search-forward
Fast word movement with Ctrl
Xterm supports moving between words with
Ctrl+Right by default. To achieve this effect with other terminal emulators, find the correct terminal codes, and bind them to
~/.inputrc. The codes can be made visible by first issuing the cat command.
For example, for urxvt:
"\eOd": backward-word "\eOc": forward-word
Mimic Zsh run-help ability
Zsh can invoke the manual for the written command pushing
A similar behaviour is obtained in Bash by appending this line in your
"\eh": "\C-a\eb\ed\C-y\e#man \C-y\C-m\C-p\C-p\C-a\C-d\C-e"
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.
Tips and tricks
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
cd when entering just a path in the shell. For example:
bash: /etc: Is a directory
But after adding one line into
... shopt -s autocd ...
[user@host ~]$ /etc cd /etc [user@host etc]$
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
shopt -s checkwinsize
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.
- Bash Reference
- Bash manual page
- 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
- Advanced Bash Scripting Guide
- Quote Tutorial