Bash

From ArchWiki
Revision as of 14:42, 25 October 2009 by Hollunder (talk | contribs) (Resources)
Jump to: navigation, search

Template:I18n links start Template:I18n entry Template:I18n entry Template:I18n entry Template:I18n links end

Bash is Arch Linux's default shell.

Tips & Tricks

History Search

Usually, pressing the <up> key will cause the last command to be shown, no matter what you 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:

Usually, when typing 'ls' and pressing <up>, your current input will be replaced with 'man mount'. If you are using history seach, only past commands beginning with 'ls' (the current input) will be shown, in this case 'ls /usr/src/linux-2.6.15-ARCH/kernel/power/Kconfig'.

You can enable this mode by adding to Template:Filename or your Template:Filename

"\e[A":history-search-backward
"\e[B":history-search-forward

Alternatively, you can use Ctrl-r (reverse-search-history), which does not search based on your previous input but instead jumps backwards in the history buffer as you type in a search term (this is called an "incremental search" in bash).

Pressing Ctrl+r again during this mode will take you to the previous line in the buffer that matches the current search term, while pressing Ctrl+g (abort) will cancel the search and take you back to the current input line. So in order to search through all your previous mount commands, press Ctrl+r, type 'mount' and keep pressing Ctrl+r until you find the line you were looking for.

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 you press Ctrl-q. (This is called XON/XOFF flow control). If you want to use forward-search-history you can either disable flow control by issuing

$ stty -xion

or you can use a different key in your Template:Filename, 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 your inputrc:

"\ew":"\C-e # macro"

Now type a line and press Alt+w. Readline will act as though you had presses Ctrl-e (end-of-line) typed ' # macro'.

You can 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 you press Alt+s

"\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 it's output:

"\e\C-b":"\C-e > /dev/null 2>&1 &\C-m"

As you can see, using readline macros can save you a lot of typing.

Enabling Advanced Bash Completion

Bash provides some simple tab-completion facilities out of the box, e.g. completing command and variable names. The bash-completion package extends this functionality by adding completion to a wide range of commands and their options. Enabling advanced bash completion is quite simple. Here's how.

# pacman -S bash-completion

Just start a new shell, and it will be automatically enabled thanks to /etc/bash.bashrc. If you use a login shell, make sure this patch is applied to /etc/profile.bash : http://repos.archlinux.org/viewvc.cgi/bash/trunk/profile.bash?r1=52872&r2=52871&pathrev=52872

Resources