Bourne Family Shell Commands Cheatsheet

Reference for sh, bash, dash, and ksh commands

Shell Overview
  • sh - The original Bourne shell, very minimal
  • bash - Bourne Again Shell (default in most Linux distros)
  • dash - Lightweight sh-compatible shell (used in Ubuntu for scripts)
  • ksh - KornShell, with scripting improvements over sh

File Operations

Basic File Commands

ls - List directory contents

ls -l # Long listing
ls -a # Include hidden files

cd - Change directory

cd /path/to/dir
cd ~ # Home directory
cd - # Previous directory

pwd - Print working directory

cp - Copy files/directories

cp file1 file2
cp -r dir1 dir2 # Recursive copy

mv - Move/rename files

mv oldname newname
mv file /target/dir/

rm - Remove files

rm file
rm -r dir # Recursive remove
rm -f file # Force remove
File Viewing/Editing

cat - Concatenate and display files

cat file.txt
cat file1 file2 > combined

less/more - View file contents page by page

less longfile.txt
more longfile.txt

head/tail - Show beginning/end of file

head -n 10 file.txt # First 10 lines
tail -f logfile # Follow log in real-time

grep - Search text patterns

grep "pattern" file.txt
grep -r "pattern" /dir # Recursive search
grep -i "pattern" file # Case-insensitive

System Information

uname - System information

uname -a # All system info
uname -s # Kernel name
uname -r # Kernel release

df - Disk space usage

df -h # Human-readable format

du - Directory space usage

du -sh /dir # Summary for directory

free - Memory usage

free -m # Show in megabytes

top/htop - Process monitoring

ps - Process status

ps aux # Show all processes
ps -ef # Full format listing

Shell Features

Variables and Parameters

VAR=value - Set variable

NAME="John"
echo $NAME

export - Make variable available to subprocesses

export PATH=$PATH:/new/path

$0-$9 - Positional parameters

$# - Number of arguments

$*/$@ - All arguments

$? - Exit status of last command

$$ - Current process ID

Input/Output Redirection

> - Redirect output (overwrite)

ls > filelist.txt

>> - Redirect output (append)

echo "new line" >> file.txt

<< /span> - Redirect input

wc -l < file.txt

| - Pipe output to another command

cat file.txt | grep "pattern" | wc -l

2> - Redirect stderr

command 2> error.log

&> - Redirect stdout and stderr (bash)

command &> output.log

Shell Scripting

Conditionals and Tests

test/[ ] - Evaluate conditions

if [ -f "$file" ]; then
  echo "File exists"
fi

Common test operators:

  • -f file - File exists
  • -d dir - Directory exists
  • -z "$var" - String is empty
  • -n "$var" - String is not empty
  • "$a" = "$b" - Strings equal
  • $num1 -eq $num2 - Numbers equal

case - Switch statement

case "$var" in
  pattern1) command1 ;;
  pattern2) command2 ;;
  *) default_command ;;
esac
Loops

for loop

for i in 1 2 3; do
  echo $i
done

while loop

while [ $count -lt 5 ]; do
  echo $count
  count=$((count+1))
done

until loop

until [ $count -ge 5 ]; do
  echo $count
  count=$((count+1))
done
Functions

Defining functions:

function_name() {
  commands
  return $?
}

Example function:

greet() {
  echo "Hello, $1"
}
greet "World"

Advanced Features

bash/dash/ksh Extensions

[[ ]] - Enhanced test (bash/ksh)

if [[ $var == *.txt ]]; then
  echo "Text file"
fi

$(()) - Arithmetic expansion

echo $(( 5 + 3 )) # Prints 8

${var%pattern} - Remove suffix

${var#pattern} - Remove prefix

filename="/path/to/file.txt"
echo ${filename%.*} # /path/to/file
echo ${filename##*/} # file.txt

${var:-default} - Use default if unset

echo ${NAME:-"Guest"} # Prints "Guest" if NAME unset
Shell Script Shebang Examples
#!/bin/sh # Original Bourne shell (most portable)
#!/bin/bash # Bourne Again Shell (common default)
#!/bin/dash # Debian Almquist Shell (fast, limited)
#!/bin/ksh # KornShell (advanced features)