Bash
From ArchWiki
Article summary |
---|
Improving Bash over its default capabilities. |
Language |
English |
Italiano |
简体中文 |
Related |
Color Bash Prompt |
Bashrc |
Zsh |
Bash is a shell/programming language made by the GNU Project. Its name is an acronym which stands for Bourne-again shell, itself being a homaging reference to its predecessor; the long-deprecated Bourne shell. Bash can be run on most UNIX-like operating systems, one of these being Linux. Arch employs Bash as its default shell environment and copiously uses it throughout its init-scripts.
Contents |
Extended usage
Miscellaneous settings and alterations that enhance Bash's usability
Completion
In spite Bash's native support for basic file name, command and variablie auto-completion, there are ways of improving and increasing its reach.
Advanced completion
The bash-completion package extends functionality by adding completion to a wide range of commands and their options. Enabling advanced bash completion is quite simple. Here is how:
# pacman -S bash-completion
Just start a new shell and it will be automatically enabled thanks to /etc/bash.bashrc.
Faster completion
By appending the following into ~/.bashrc:
set show-all-if-ambiguous on
it is no longer necessary to hit Tab twice to produce a list of all possible completions, as a single key-press will suffice.
History search
Usually, pressing the up key will cause the last command to be shown regardless of what has been typed so far. However, many users find it more practical to only past commands that begin with the current input.
Consider this list of commands:
- ls /usr/src/linux-2.6.15-ARCH/kernel/power/Kconfig
- who
- mount
- man mount
In this situation, when typing ls and pressing up, current input will be replaced with man mount; the last performed command. Had history seach been enabled, only past commands beginning with l' (the current input) would've been shown, in this case ls /usr/src/linux-2.6.15-ARCH/kernel/power/Kconfig.
Enable this mode by adding to /etc/inputrc or ~/.inputrc:
"\e[A":history-search-backward "\e[B":history-search-forward
Alternatively, use Ctrl+r (reverse-search-history), which does not search based on previous input but instead jumps backwards in the history buffer as commands are typed in a search term (this is called an "incremental search" in bash).
Pressing Ctrl+r again during this mode will display the previous line in the buffer that matches the current search term, while pressing Ctrl+g (abort) will cancel the search and restore the current input line. So in order to search through all previous mount commands, press Ctrl+r, type 'mount' and keep pressing Ctrl+r until find the desired line.
The forward equivalent to this mode is called forward-search-history and is bound to Ctrl+s by default. Beware though that most terminals override Ctrl+s to suspend execution until Ctrl+q is entered. (This is called XON/XOFF flow control). For activating forward-search-history, either disable flow control by issuing:
$ stty -ixon
or use a different key in inputrc, for example Alt+s, which is not bound by default:
"\es":forward-search-history
Readline macros
Readline also supports binding keys to keyboard macros. For simple example, run this command in bash:
bind '"\ew":"\C-e # macro"'
or add the part within single quotes to inputrc:
"\ew":"\C-e # macro"
Now type a line and press Alt+w. Readline will act as though Ctrl+e (end-of-line) had been pressed, appended with ' # macro'.
Use any of the existing keybindings within a readline macro, which can be quite useful to automate frequently used idioms. For example, this one makes Ctrl+Alt+l append "| less" to the line and run it (Ctrl+m is equivalent to Enter:
"\e\C-l":"\C-e | less\C-m"
The next one prefixes the line with 'yes |' when pressing Ctrl+Alt+y, confirming any yes/no question the command might ask:
"\e\C-y":"\C-ayes | \C-m"
This example wraps the line in su -c '' if Alt+s is pressed:
"\es":"\C-a su -c '\C-e'\C-m"
As a last example, quickly send a command in the background with Ctrl+Alt+b, discarding all its output:
"\e\C-b":"\C-e > /dev/null 2>&1 &\C-m"
Resources
- Advanced Bash Scripting Guide - Very good resource regarding shell scripting using bash
- Bash Reference Manual - Official reference (654K)
- Bash Scripting by Example
- Completion Guide
- Greg's Wiki - Highly recommended
- man page
- Readline Guide
- Quote Tutorial