Shell Cheatsheet

Assigning Variables

#!/bin/sh
length=80
chapter=chap # no spaces
title="Greetings from Space" # with spaces
empty= # empty variable
size=$length

Displaying Variables

#!/bin/sh
echo $length # 80
echo length=$length # length=80
echo this is $title # this is Greetings from Space
echo ${chapter}5 # chap5 - to display with no space between variable and other parts of string

Special Variables

#!/bin/sh
$? # exit status of the last command
$$ # process number of the current shell
$! # process number of the last background command
$0 # filename of the current script
$# # number of arguments
$n # arguments (n = 1, 2, ...)
$@ # equivalent of $1 $2 ... $n
$* # same as $@

Special Symbols and Escaping

  • Double quotes escape special characters, except for $, `, \c (c = any character)
  • Single quotes escapes $ as well
  • Backslash escapes single character
  • Combinations with backsllash:
    • \n – new line
    • \c – stay on the same line
    • \t – tab

#!/bin/sh
echo * # equivalent to ls (print out current dir)
echo "*" # prints *
echo "$length" # prints 80 - $ is not escaped by double quotes
echo '$length' # prints $length
echo \$length # prints $length - escapes single character

Reverse quotes (`command`) – run command:

#!/bin/sh
echo today is `date` # shows today's date
users=`who | wc -l`
echo $users # displays number of users logged in

Arguments Shift

#!/bin/sh
# script called like this:
# thisscript hello world
a=$1; shift
b=$1
echo $b $a # world hello

Operators

&& - second command will only run if the first one was successful
|| - second command runs only if first one fails

#!/bin/sh
test -d ~/b && echo "b is directory" # will show "b is directory" only if b is directory
test -f ~/b || echo "b is not file, therefore b is directory" # equivalent to above

For and list

#!/bin/sh
for i in here there everywhere # list
do
   echo $i
done
# will print 
# > here
# > there
# > everywhere

Lists are separated by ; or new line

If-else

#!/bin/sh
if [condition1]
then
    echo "condition 1"
else
    echo "condition 2"
fi

# or
if [condition1]; then
    echo "condition 1"
else
    echo "condition 2"
fi
Conditions
  • -a - and
  • -o - or
  • -gt - greater then
  • -lt - less then
  • -eq - equal
  • -ne - not equal
  • -ge - greater or equal
  • -le - less or equal

#!/bin/sh
if [ $i -gt 7 -a $i -le 99] #$i between 7 and 99

While and Until

#!/bin/sh
# While
while [condition]; do
  [command]
done

# Until
until [condition]; do
  [command]
done

Case

#!/bin/sh
case $i
in
  1) continue;;
  2) echo $i
     continue;;
  3) break;;
esac

Arithmetic Operations (expr)

#!/bin/sh
n=0
n=$n+1
echo $n # prints 0+1 - treats as string by default

n=0
n=`expr $n+1`
echo $n # prints 1

n=`expr 2+3`
echo $n # prints 5
expr 5 "*" 6 # 30
Shell Cheatsheet

How to calculate and print number of files in each subfolder of the current folder on Linux

find . -type d | while read DIR; do find $DIR -type f | printf "%-8s %s\n" $(wc -w) $DIR; done | sort -nr

That is: find all subdirectories of current directory; for each subdirectory, find all files, and print file count and path. Finally it sorts the result to show folders with most files on top
Note that the first entry is the folder itself. Also this script omits soft links and does not count sub-folders as files.

For example:

[someone@somewhere]# cd /etc/X11/
[someone@somewhere]# find . -type d | while read DIR; do find $DIR -type f | printf "%-8s %s\n" $(wc -w) $DIR; done | sort -nr
10 .
7 ./xinit
3 ./xinit/xinitrc.d
0 ./xorg.conf.d
0 ./xinit/Xclients.d
0 ./fontpath.d
0 ./applnk

How to calculate and print number of files in each subfolder of the current folder on Linux