Updated at 2013-11-03 00:51

Linux shell is command-line interpreter or command processor. A shell is a simple domain-specific programming language. Shell is the traditional Unix user interface, writing commands. Shells can also load prewritten commands from files called scripts.

Bourne-again Shell (bash)

  • Most widely used Unix shell.
  • Installed by default on Linux and OS X.
  • Can be used on Windows with Cygwin and MinGW.
  • Can be used on Android with GNU Bash Installer.
  • Script files use .sh extension or no extension at all.
  • You can execute scripts by specifying relative or absolute path to the script.

Script Basics

Scripts start with shebang #! that specifies what interpreter to use. Full syntax is #!interpreter [optional-arguments]

#!/usr/bin/php          (execute using PHP)
#!/usr/bin/ruby         (execute using Ruby)
#!/usr/bin/python -O    (execute using Python with optimizations)

Shebangs must specify absolute path to the interpreter. You can try to use env utility to get the absolute path.

#!/usr/bin/env sh

Understand script search paths. Scripts are searched from directories specified in an environmental variable named PATH.

# Show what is your $PATH.
echo $PATH

Scripts need to have execution permissions to run.

# Give user permission to read and execute the script.
chmod u+rx <SCRIPT_FILE>

# Give group and other permission to execute the script.
chmod 711 <SCRIPT_FILE>

Do batch processing using non-global scripts. For example, store a script in the project folder and run from there.

echo compiling...
cc -c foo.c
cc -c bar.c
cc -c qux.c
cc -o myprog foo.o bar.o qux.o
echo done.

Use functions for readability.

# bad
[[ -z $dir ]] \
    && do_something...

# good
is_empty() {
    local var=$1
    [[ -z $var ]]
is_empty $dir \
    && do_something...
# good
ExtractBashComments() {
    egrep "^#"
cat | ExtractBashComments | wc
comments=$(ExtractBashComments <

Favor $() over backticks ` in scripts. I personally use backticks while writing to command line as it's faster.

# both commands below print out: A-B-C-D
echo "A-`echo B-\`echo C-\\\`echo D\\\`\``"
echo "A-$(echo B-$(echo C-$(echo D)))"

Utilize $() as part of a command.

sleep 30 &
sleep 30 &
sleep 30 &
pgrep sleep             # => returns the process id by process name
kill `pgrep sleep`      # => kills all matching
kill `pgrep -n sleep`   # => kills the newest matching command

Always quote variables with double quotation marks. For consistency.

echo "${VAR}a"

Scripts are not programs. If your script is over hundred lines, you are doing something wrong.

Bash Configuration

There are multiple configuration files that affect how your bash behaves.

Login Shell: When logging in with console e.g. SSH session, you start a login shell. Login shells execute .profile configuration file in your home directory. That may then include .bashrc inside the .profile. Login shell will try to find .bash_profile > .bash_login > .profile and only executes the first one it finds.

Non-login Shell: When you open a terminal application e.g. xterm, you start a non-login shell, except in OS X, the it is always login shell. Non-login shells execute .bashrc configuration file in your home directory.

All in all, .bashrc is the best place for all bash configurations as it is always loaded. You should use .profile to all non-bash related settings e.g. environmental variables. You should use .bash_profile to load .profile on OS X for consistency.

# .profile
# Add path to Python scripts directory.

Bash configuration file examples.

# .bashrc

# General
alias editbash="open ~/.bashrc"
alias ps?="ps aux | grep"

# Sublime
alias s.="subl ."

# Git
alias gl='git log --pretty=format:"%h%x09%an%x09%s"'
alias gs="git status"
alias ga="git add ."
alias gb="git branch"
alias gc="git commit"
alias gcm="git commit -m"
alias gca="git commit --amend"
alias gpp="git pull; git push"
alias gpl="git pull"
alias gps="git push"
alias grh="git reset --hard HEAD"
# Usage: coco <COMMIT_MESSAGE>;
coco() {
    git add .;
    git commit -m "$1";
    git pull;
    git push;

alias tphp="tail -f /Applications/MAMP/logs/php_error.log"

alias pubkey="more ~/.ssh/ | pbcopy | printf \
'=&gt; SSH public key copied to clipboard.\n'";

Bash Script Examples


# Only thing global are readonly values, functions and the main function call.

readonly SCRIPT_NAME=$(basename $0)
readonly SCRIPT_DIR=$(cd $(dirname "$0"); pwd)
readonly SCRIPT_ARGS="$@"

is_file() {
    local file=$1
    [[ -f $file ]]

is_dir() {
    local dir=$1
    [[ -d $dir ]]

print_importants() {
    echo "Here are the script variables:"
    echo ${SCRIPT_NAME}
    echo ${SCRIPT_DIR}
    echo ${SCRIPT_ARGS}

main() {
    local file
    local files_in_same_dir=${SCRIPT_DIR}/*
    for file in $files_in_same_dir
        # Max one thing per line, use `/` to separate the lines.
        is_dir $file \
            && echo "${file} is a directory."
        is_file $file \
            && echo "${file} is a file."


echo "Last program return value: $?"
echo "Script's PID: $$"
echo "Number of arguments: $#"
echo "Scripts arguments: $@"
echo "Scripts arguments separeted in different variables: $1 $2..."

Default value if nothing provided.



if [ $PWD = "/" ]
     echo "You are at the root.";
# elif [ toinen ehto ]
     echo "You are at $PWD";
case "$VARIABLE" in
    0) echo "There is a zero.";;
    1) echo "There is a one.";;
    *) echo "It is not null.";;


for i in `seq 3`
    echo "$i"
# From 1 to 5.
for i in {1..5}
   echo "Welcome $i times"
# C-style for-loop from 1 to 5.
for (( c=1; c<=5; c++ ))
   echo "Welcome $c times"
# For each file.
for file_path in `ls ./`
    echo $file_path
    file_basename=`basename "$file_path" .txt`
    echo $file_basename
# Create backup on each specified file if no .bak exists.
for f in $FILES
    # If .bak backup file exists, read next file.
    if [ -f ${f}.bak ]
        echo "Skipping $f file..."
    # No backup file exists, use cp command to copy file.
    /bin/cp $f $f.bak


function foo ()
    echo "Arguments work just like script arguments: $@"
    echo "And: $1 $2..."
    echo "This is a function"
    return 0
foo "My name is" $NAME

Convert JPGs to PNGs


# Use $jpg in place of each filename given.
for jpg; do

    # Find the PNG version of the filename by replacing .jpg with .png.
    echo converting "$jpg" ...

    #Uuse the common convert program to create the PNG.
    if convert "$jpg"
        # If it worked, rename the temporary PNG image to the correct name.
        mv "$png"
        # Otherwise complain and exit from the script.
        echo 'Error: failed output saved in "".' >&2
        exit 1

echo "All conversions successful"

String Substitution

echo ${VARIABLE/Some/A}
# Replaces first occurrence of "Some" with "A".

String Interpolation

# `` makes string a command and is replaced with response.
echo "Hello, `whoami`!"

# $ means "this is a variable"
NAAAAME="Hey, `whoami`!"
echo "You are at $PWD."

Parameter Operations


# Trim the shortest match from the end.
# ${variable%pattern}

# Trim the longest match from the beginning
# ${variable##pattern}

# Trim the longest match from the end
# ${variable%%pattern}

# Trim the shortest match from the beginning
# ${variable#pattern}

# Return the length of the variable in characters.
# ${#variable}


file_path = ${file%/*}
echo file_path
# -> /tmp/my.dir

file_name = ${file##*/}
echo file_name
# -> filename.tar.gz

file_basename = ${file_name%%.*}
echo file_basename
# -> filename

file_extension = ${file_name#*.}
echo file_extension
# -> tar.gz

echo "The length of your file_path is ${#file_path}"

export API_KEY="1234"
if [ ${#API_KEY} != 3 ]; then
    echo "You have entered a wrong API key!"
    #return 0

Download Sequence

for i in `seq -f"%03g" 1 104`
    wget -c "$i.ogg"



# Create temporary directory.
scratch=$(mktemp -d -t tmp.XXXXXXXXXX)

cp "$scratch/merged.tar.bz2" "$1"

# Define what to do one exit.
function finish {
    rm -rf "$scratch"

# Run finish on exit.
trap finish EXIT

SVN Update Script


# Change current working directory, while saving the original one.
pushd /srv/www/my-web-project

# Update whole project
svn update

# Echo version.
# | Cut from delimiter :
# | Deleting instances of MS (translate command)
# > Save to revision.txt
svnversion | cut -d: -f | tr -d MS > revision.txt

# Return to original working directory.

Check if executable exists.

# -s  = retuns 1 if command is found, 0 if not.
which -s curl && echo lol
function if_cmd_found() { which -s $1; }
if_cmd_found "curl" && echo "Hello!"

Adding optional debug messages to scripts.

# Debug information
function debug() {
    if [[ $DEBUG ]]
        echo ">>> $*"
# debug "Doing something."
# export DEBUG=true

You can use Heredoc to work with long messages.

cat << EOF

Usage: myscript <command> <arguments>

Version: 1.0

Available Commands:

    install - Install package
    uninstall - Uninstall package
    update - Update package
    list - List packages


Colored echoes.

# Colored echoes.
NORMAL=$(tput sgr0)
GREEN=$(tput setaf 2; tput bold)
YELLOW=$(tput setaf 3)
RED=$(tput setaf 1)
function red() {
    echo -e "$RED$*$NORMAL"
function green() {
    echo -e "$GREEN$*$NORMAL"
function yellow() {
    echo -e "$YELLOW$*$NORMAL"