cs.thefarshad
medium

Shell Scripting

Automate your workflow by combining commands into reusable scripts with variables and loops.

So far, we’ve typed commands one by one. Shell scripting allows you to save a sequence of commands into a file, add logic (like if statements and loops), and run them as a single program.

cs shell — type "help" to see commands.
learner@cs:~$ 
A simulated shell — supports pipes ( | ) and redirection ( > , >> ). Use ↑/↓ for history.

A Simple Script

A shell script is just a text file. It usually starts with a shebang (#!) telling the system which shell to use.

#!/bin/bash
# This is a comment

NAME="Learner"
echo "Hello, $NAME!"
echo "Today is $(date)"

Variables and Logic

  • Variables: No spaces around the =. Access them with $.
  • Arguments: $1, $2, etc., refer to the first, second arguments passed to the script. $# is the count of arguments.
  • Conditionals:
    if [ $USER == "root" ]; then
      echo "Welcome, admin."
    else
      echo "Standard user."
    fi

Loops

Automating a task over many files is where scripting shines:

# Rename all .txt files to .bak
for file in *.txt; do
  mv "$file" "${file%.txt}.bak"
done

Why Script?

  1. Automation: Never type a complex 5-command pipe more than once.
  2. Reproducibility: Document exactly how a build or deployment is performed.
  3. Power: Combine small, specialized tools (grep, sed, awk) into a custom solution for your specific data.

Takeaways

  • Scripts are executable files containing a sequence of shell commands.
  • They support standard programming constructs like variables, loops, and logic.
  • They are the “glue” that holds together complex server-side workflows.