Thanks for all your error reports, I didn't forget it. I'll cleanup my guide soon. Thanks again!

Bash

Our beloved shell, we use and abuse it every day. There are several general tips which will help you to work with without accidentally jeopardizing your box.

sh cheatsheet

Control Operators:
    & && ( ) ; ;; | || <newline>
Redirection Operators:
    < > >| << >> <& >& <<- <>
Reserved Words:
    !     elif    fi      while   case
    else  for     then    {       }
    do    done    until   if      esac
Quoting:
    "    '    \
Special Parameters:
    * @ # ? - $ ! 0
Grouping Commands Together:
    {} ()
    { printf " hello " ; printf " world\n" ; } > greeting
Functions:
    name () command
    local [varialbe | -] ...
Word Expansions:
    1. Tilde Expansion, Parameter Expansion, Command Substitution, Arithmetic Expansion (these all occur at the same time).
    2. Field Splitting is performed on fields generated by step (1) unless the IFS variable is null.
    3. Pathname Expansion (unless set -f is in effect).
    4. Quote Removal.
    The $ character is used to introduce parameter expansion, command substitution, or arithmetic evaluation.
Parameter Expansion:
    ${expression}        ${parameter}            ${parameter:-word}
    ${parameter:=word}   ${parameter:?[word]}    ${parameter:+word}
    ${#parameter}        ${parameter%word}       ${parameter%%word}
    ${parameter#word}    ${parameter##word}
Command Substitution:
    $(command)           `command`
Arithmetic Expansion
    $((expression)):
White Space Splitting (Field Splitting):
    IFS
Pathname Expansion (File Name Generation):
    Each word is viewed as a series of patterns, separated by slashes.
Shell Patterns:
    Meta-characters are “!”, “*”, “?”, and “[”.
Builtins:
    :         true        . file     alias
    bd        command     cd -       cd -LP
    echo      eval        exec       exit
    export    export -p   fc         fg
    getopts   hash        pwd        read
    readonly  readonly -p printf     set
    shift     test        times      trap
    type      ulimit      umask      unalias
    unser     wait
Environment:
    HOME      PATH        CDPATH     MAIL
    MAILCHECK MAILPATH    PS1        PS2
    PS4       IFS         TERM       HISTSIZE
    PWD       OLDPWD      PPID
Files:
    $HOME/.profile        /etc/profile

General tips

# Think before you type
    # When rebooting, stop and say to yourself "I'm going to reboot the server foo,
    # I mailed everyone interested so it's okay, now I'll press enter and pray!"
    reboot
 
    # Before doing anything potentially harmful, stop and say to yourself "I'm going to
    # delete dir /foo/bar, it's okay because it contains old unnecessary files,
    # I'm sure of it."
    rm -rvf /foo/bar # still wrong because of next tip
 
    # Always do ls -R /foo/bar before deleting, chmodding, moving
    # or doing anything potentially harmful!
    ls -R /foo/bar
    rm -rvf /foo/bar # now it's ok
 
# Always use -v key
    ln -v
    mv -v
    chmod -v
    cp -v
# Always specify full path
    rm -rf /full/path/to/dir
    chmod -R foo:bar /full/path/to/dir
    <...>
 
# Don't do this EVAR!
    rm -rf .* # bad bad bad thing will happen to you!
    chmod -R foo:bar .*

Usefull snippets and bash quirks

# Getting list of files
    # Don't do this, brakes on spaces
    for path in `find`; do
        stuff with "$path" # too late - already broken on whitespace
    done
 
    # Do this, it's safe
    find | while read path;
        do stuff with "$path"
    done
 
# Copy binary file using bash builtins ONLY
( while read -r -d '' ; do printf %s'\0' "${REPLY}" ; done ;  printf %s "${REPLY}" ) < /bin/ls > /tmp/ls
 
# Doing something in loop
    while true ; do stuff ; done
 
# Doing something in loop iterating through ranges
    for ((i=1;i<=255;++i)); do host 10.0.0.$i; done
 
# Using bash as a calculator
    echo $((10*1024**3))
 
# readable format
    echo $(/bin/date +%d-%m-%Y)
    31-05-2012
 
# good format for archive directory naming, by default sorts first-to-last
    echo $(/bin/date +%Y%m%d)
    20120531
 
# control teh cursor!
    tput cup 10 10
 
# get input from user
    read name < /dev/tty
 
# conditions
    http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
 
# xtrace (debugging)
    bash -x

Discussion

Navigation

Learn Linux The Hard Way