Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary end Bash (Bourne-again Shell) is a 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. Arch copiously uses Bash throughout its init-scripts.
- 1 Configuration
- 2 Tips and tricks
- 2.1 Prompt customization
- 2.2 Aliases
- 2.3 Auto-completion
- 2.4 History search
- 2.5 Disable control-z in terminal
- 2.6 Disabling control echo
- 2.7 Readline macros
- 2.8 Command-line editing
- 2.9 Startup files
- 2.10 Clear the screen after logging out
- 2.11 ASCII art, fortunes and cowsay
- 2.12 ASCII Historical Calendar
- 3 Resources
- Template:Filename (Non-standard: only some distros, Arch included)
These files are sourced by bash in different circumstances. When an interactive and login shell is started, it will source Template:Filename, and the first of Template:Filename, Template:Filename and Template:Filename that exists and is readable. Bash will source Template:Filename upon exit. However, when a non-login interactive shell is started, it will only source Template:Filename and Template:Filename. Examples of the user dotfiles can be found in Template:Filename.
An overview of the commonly used configuration files:
This file is read and sourced by bash when an interactive login shell is started.
The file Template:Filename is read and sourced by bash when a non-login interactive shell is started, for example, when you open a virtual console from the desktop environment. This file is useful for setting up a user-specific shell environment.
Template:Moveto All of these should be exported in order to become environment variables instead of shell variables:
VARIABLE=content export VARIABLE
The web browser. Helpful to set in an interactive shell configuration file so that it may be dynamically altered depending on the availability of a graphic environment, such as X:
if [ -n "$DISPLAY" ]; then BROWSER=firefox else BROWSER=links fi
Should point to a lightweight text editor, such as ed or ex. Convenient for editing SCM commit logs that don't require a whole lot of features from the application. ex has more features, but it will clear the whole screen, while ed does not.
FTP and HTTP proxy
FTP and HTTP proxy server, respectively:
The location of incoming email. The traditional setting is Template:Filename.
The pager called by man and other applications; for example, less, or more.
The PATH variable is a string delimited by colons (:) which specifies the directories to search for when users enter a command name without an explicit path preface. If the command is found in the directories specified in PATH, the program is executed.
Tips and tricks
The bash prompt is governed by the variable $PS1. To colorize the bash prompt, first comment out the default $PS1:
#PS1='[\u@\h \W]\$ '
Then add the following line:
PS1='\[\e[0;31m\]\u\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \[\e[0;31m\]\$ \[\e[m\]\[\e[0;32m\] '
This $PS1 is useful for a root bash prompt, with red designation and green console text. For details on customizing your bash prompt, see Color Bash Prompt.
An Alias substitutes a word with a user-defined string, this is useful for creating abbreviations of a command or adding default arguments to a command.
For example, to make rm prompt before every removal:
alias rm='rm -i'
or once before removing more than three files and when removing recursively
alias rm='rm -I'
To make cp and mv prompt for overwrite:
alias cp='cp -i' alias mv='mv -i'
To use vi instead of nano (to force someone to learn vi):
It is useful to have the auto-complete feature (pressing Tab key twice on the keyboard) after you type some command like sudo.
To do this add line in this format to your Template:Filename file:
complete -cf your_command
For example, to enable auto-complete after sudo and man:
complete -cf sudo complete -cf man
In spite of bash's native support for basic file name, command and variable auto-completion, there are ways of improving and extending its reach.
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, just install the following package:
# pacman -S bash-completion
Start a new shell and it will be automatically enabled thanks to Template:Filename.
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.
Usually, pressing the up arrow key will cause the last command to be shown regardless of what has been typed so far. However, users may find it more practical to list only past commands that match the current input.
For example, if the user has typed the following commands:
In this situation, when typing Template:Codeline and pressing the up arrow key, current input will be replaced with Template:Codeline, the last performed command. Had history search been enabled, only past commands beginning with Template:Codeline (the current input) would've been shown, in this case Template:Codeline.
Alternatively, one can use reverse-search-history (incremental search) by pressing Template:Keypress+Template:Keypress, which does not search based on previous input but instead jumps backwards in the history buffer as commands are typed in a search term. Pressing Template:Keypress+Template:Keypress again during this mode will display the previous line in the buffer that matches the current search term, while pressing Template:Keypress+Template:Keypress (abort) will cancel the search and restore the current input line. So in order to search through all previous Template:Codeline commands, press Template:Keypress+Template:Keypress, type 'mount' and keep pressing Template:Keypress+Template:Keypress until the desired line is found.
The forward equivalent to this mode is called forward-search-history and is bound to Template:Keypress+Template:Keypress by default. Beware that most terminals override Template:Keypress+Template:Keypress to suspend execution until Template:Keypress+Template:Keypress 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 Template:Filename. For example to use Alt+S, which is not bound by default:
If you repeat the same command several times, they will all be appended in your history. To prevent this, add to your Template:Filename:
To disable logging blank commands add this to your Template:Filename:
If your Template:Filename already contains
replace it with
Disable control-z in terminal
You can disable ctrl-z(pauses/closes your CLI application) feature for you CLI by wrapping your command in this script
#!/bin/bash trap "" 20 /path_to_your_application/
#!/bin/bash trap "" 20 /usr/bin/adom
With example script when you accidentially press ctrl+z instead of shift+z or some other key combo while playing Adom(game) your game won't end. Nothing will happen because ctrl+z will be ignored.
Disabling control echo
Due to an update to Template:Package Official, the terminal now echoes Template:Codeline after Template:Keypress+Template:Keypress is pressed. For users who wish to disable this, simply add the following to Template:Filename:
set echo-control-characters off
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 'Template:Codeline'.
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"
set -o vi
Taken from www.catonmat.net
There are different possibilities:
- if login shell → Template:Filename then the first readable of Template:Filename, Template:Filename, and Template:Filename
- if interactive + non-login shell → Template:Filename then Template:Filename
- if login shell + legacy mode → Template:Filename then Template:Filename
But, in Arch, by default:
- Template:Filename (indirectly) sources Template:Filename
- Template:Filename which users are encouraged to copy to Template:Filename, sources Template:Filename
Clear the screen after logging out
To clear the screen after logging out on a virtual terminal, append the following lines to Template:Filename:
ASCII art, fortunes and cowsay
Along with a colors, system info and ASCII symbols, BASH can be made to display a piece of ASCII art on login. ASCII images can be found online and pasted into a text file, or generated from scratch. To set the image to display in a terminal on login, place the string
cat /path/to/text/file at the top of Template:Filename.
Random poignant, inspirational, silly or snide phrases can also be shown. To see an example, install the fortune-mod package from the extra repository.
To have a random phrase displayed when logging into a terminal, just set
command fortune as the top line in Template:Filename.
The earth is like a tiny grain of sand, only much, much heavier. ----------------------------------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
The ASCII images are generated by Template:Filename text files located in Template:Filename, and all themes can be listed with the command
cowsay -l These files can be edited to the user's liking; custom images can also be created from scratch or found on the net. The easiest way create a custom cow file from an image found online would be to open an existing Template:Filename file in a text editor, copy-and-paste the image from a browser and save the file as a different name. Test the custom file using
# cowsay -f Template:Filename $(fortune)
This can produce some nice eye candy, and the commands used can be more complex. For a specialized example, take a look here.
________________________________________ ( Fry: I must be a robot. Why else would ) ( human women refuse to date me? ) ---------------------------------------- o o o ,'``.._ ,'``. :,--._:)\,:,._,.: :`--,@@@:`...';\ `,'@@@@@@@`---'@@`. /@@@@@@@@@@@@@@@@@: /@@@@@@@@@@@@@@@@@@@\ ,'@@@@@@@@@@@@@@@@@@@@@:\.___,-. `...,---'``````-..._@@@@|:@@@@@@@\ ( )@@@;:@@@@)@@@\ _,-. `. (@@@//@@@@@@@@@@`'@@@@\ : `.//@@)@@@@@@)@@@@@,@; |`. _,'/@@@@@@@)@@@@)@,'@,' :`.`-..____..=:.-':@@@@@.@@@@@_,@@,' ,'\ ``--....-)=' `._,@@\ )@@@'``._ /@_@`. (@) /@@@@@) ; / \ \`-.' (@@@`-:`. `' ___..'@@_,-' |/ `.) `-. `.`.``-----``--,@@.' |/`.\`' ,','); ` (/ (/ (user@host)-(10:10 AM Wed Dec 22)--(~))--->
ASCII Historical Calendar
To install calendar files in your
$ mkdir -p ~/.calendar $ curl -o calendar.rpm http://download.fedora.redhat.com/pub/epel/5/x86_64/calendar-1.25-4.el5.x86_64.rpm $ rpm2cpio calendar.rpm | bsdtar -C ~/.calendar --strip-components=4 -xf - ./usr/share/c*
This will then print out the calendar items
$ sed -n "/$(date +%m\\/%d\\\|%b\*\ %d)/p" $(find ~/.calendar /usr/share/calendar -maxdepth 1 -type f -name 'c*' 2>/dev/null);
- 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
IRC #bash channel is a very good place to ask question about bash with a quite good activity all the time and skilled & nice people. --Sputnick 14:09, 27 July 2011 (EDT)