Bash
From Wsms
Note: This page should be titled bash (all lowercase). It is bash due to technical limitations of Mediawiki.
bash is the default shell in most Linux distributions.
Contents |
common usage
Open a terminal window and start typing
tips
The default shell for each user is set in /etc/passwd. See 20061018.
common aliases
Many people add these aliases to their ~/.bashrc:
# User specific aliases and functions alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' export VISUAL=emacs export PATH=$PATH:~/bin
customize your prompt
The prompt is customized through the PS1, PS2, PS3 ... shell variables.
The typical setting for $PS1 is:
${debian_chroot:+($debian_chroot)}\u@\h:\w\$
Which translates to something like:
ggeller@grant:~/Desktop/johnson-20050927.bu$
Sometimes the prompt is too long. So, reset it for the session by changing the value of PS1 to something like a $ or the basename of the current working directory.
ggeller@grant:~/Desktop/johnson-20050927.bu$ export PS1='$ ' $ export PS1='\W $ ' johnson-20050927.bu $
redirecting output
This command sends the output to a file and the screen at the same time:
ggeller@harrison:~$ find /root 2>&1 | tee find.out
Notice that the error-output is also visible in both the screen and the saved file.
Sometime redirection can be confusing. Here is an example from man bash:
Note that the order of redirections is significant. For example, the command
ls > dirlist 2>&1 # correct
directs both standard output and standard error to the file dirlist, while the command
ls 2>&1 > dirlist # incorrect
directs only the standard output to file dirlist, because the standard error was duplicated as standard output before the standard output was redirected to dirlist.
.bashrc and friends
Most users customize their bash environment by modifying .bashrc and related scripts in their home directory. Here are my versions as of May 2010:
.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# don't put duplicate lines in the history. See bash(1) for more options
export HISTCONTROL=ignoredups
# ... and ignore same sucessive entries.
export HISTCONTROL=ignoreboth
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(lesspipe)"
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color)
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
;;
*)
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
;;
esac
# Comment in the above and uncomment this below for a color prompt
#PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"'
;;
*)
;;
esac
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# enable color support of ls and also add handy aliases
if [ "$TERM" != "dumb" ]; then
eval "`dircolors -b`"
alias ls='ls --color=auto'
#alias dir='ls --color=auto --format=vertical'
#alias vdir='ls --color=auto --format=long'
fi
# some more ls aliases
#alias ll='ls -l'
#alias la='ls -A'
#alias l='ls -CF'
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
export VISUAL='emacs -nw'
# GGG 20080408 set PATH so it includes user's private bin if it exists
#if [ -d ~/bin ] ; then
# PATH=~/bin:"${PATH}"
#fi
.bashrc_aliases
# .bash_aliases # GGG 20090417 alias rm='rm -i' alias cp='cp -i' alias mv='mv -i'
.bash_profile
# ~/.bash_profile: executed by bash(1) for login shells.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
# the default umask is set in /etc/login.defs
#umask 022
# include .bashrc if it exists
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# set PATH so it includes user's private bin if it exists
if [ -d ~/bin ] ; then
PATH=~/bin:"${PATH}"
fi
.bash_logout
# ~/.bash_logout: executed by bash(1) when login shell exits.
# when leaving the console clear the screen to increase privacy
if [ "$SHLVL" = 1 ]; then
[ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
fi
.profile
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
see also
20061009
20061010
20061011
20061018
20061019
20061020
20070323a
Factorial_bash_script
http://www.oreilly.com/catalog/bash3/ "Learning the Bash Shell, 3rd Edition"
http://www.kernel-panic.org/Members/pacneil/beginner/node39.html
http://tldp.org/LDP/abs/html/ Advanced bashed scripting guide
Learning the bash Shell, 3rd Edition By Cameron Newham
