Skip to content

Linux Shell - Scripting

A shell script is simply a text file containing commands that you could type yourself. The power comes from chaining commands together and reusing them.

Think of it like recording a macro in Excel: instead of repeating steps by hand, you write them down once and run them whenever needed.

  1. Shebang line: tells Linux which interpreter to use.

    #!/bin/bash

    Without this, the system might use /bin/sh (which may behave differently).

  2. Commands: everything you would type manually.

  3. Comments: begin with #. Comments are ignored by the shell but help humans understand.

  1. Save it as myscript.sh.
  2. Make it executable:
    Terminal window
    chmod +x myscript.sh
  3. Run it:
    Terminal window
    ./myscript.sh
    The ./ ensures the shell looks in the current directory.

Alternatively, you can run it explicitly with an interpreter:

Terminal window
bash myscript.sh

Variables let you store values.

  • Create:
    Terminal window
    name="Alice"
  • Use:
    Terminal window
    echo "Hello $name"

By default, variables exist only inside the current shell. If you want them to be inherited by child processes, you need export:

Terminal window
export PATH=$PATH:/usr/local/bin

Scripts can accept inputs. This is essential if you want them to be reusable.

  • $0 → script name
  • $1, $2 … → first, second argument
  • $@ → all arguments

Example:

#!/bin/bash
echo "This script is: $0"
echo "First arg: $1"
echo "All args: $@"

Run it:

Terminal window
./myscript.sh apple banana

Output:

This script is: ./myscript.sh
First arg: apple
All args: apple banana

This is similar to function parameters in other languages.

Every command returns a number ($?).

  • 0 means success.
  • Anything else means an error.

This is how you can make scripts react to success or failure.


Scripts become powerful when they can make decisions. That’s where conditions come in.

Terminal window
if [ condition ]; then
echo "True"
else
echo "False"
fi

The [ ] is actually a command called test.

The test command can check properties of files:

  • -f file → exists and is a regular file.
  • -d dir → exists and is a directory.
  • -x file → file exists and is executable.

Example:

Terminal window
if [ -f /etc/passwd ]; then
echo "passwd file exists"
fi

This is used all the time in installation or configuration scripts to check whether a file or directory already exists before overwriting it.

  • [ "$a" = "$b" ] → strings equal
  • [ "$a" != "$b" ] → strings not equal
  • [ 5 -gt 3 ] → greater than
  • [ 2 -le 7 ] → less or equal

Instead of many if statements, you can use case to handle multiple possibilities:

Terminal window
case $1 in
start) echo "Starting service" ;;
stop) echo "Stopping service" ;;
*) echo "Usage: $0 {start|stop}" ;;
esac

This is often used for system scripts where the user might type ./script.sh start or ./script.sh stop. It improves readability compared to multiple if/else blocks.


Loops let you repeat actions automatically.

Best for iterating over lists.

Terminal window
for file in *.txt; do
echo "Processing $file"
done

This is equivalent to PowerShell’s foreach ($f in Get-ChildItem).

Run as long as a condition is true.

Terminal window
count=1
while [ $count -le 5 ]; do
echo "Count = $count"
count=$((count + 1))
done

This is like PowerShell’s while ($true) { ... }, except you normally put a real condition instead of $true.

The opposite of while: run until the condition becomes true.

Terminal window
until [ -f /tmp/stopfile ]; do
echo "Waiting..."
sleep 5
done

This is useful for waiting on a process to finish, or a file to appear, without writing complicated checks.