Shell scripting basics: variables, conditionals, loops, functions, shebang

Linux Essentials Preparation · lesson 11 of 23 · 25 min

Shell Scripting Fundamentals

A shell script is a text file containing a sequence of shell commands. Scripts automate repetitive tasks, system administration, and deployments.

Shebang (#!) — the first line of a script specifying the interpreter. #!/bin/bash tells the system to use bash. #!/usr/bin/env python3 uses env to find python3.

Making a script executable: chmod +x script.sh then run with ./script.sh.

Comments: Lines starting with # are comments (except the shebang). # This is a comment.

Variables: No spaces around =. name="Alice". Access with $: echo $name or echo "${name}" (braces for clarity).

Special variables: $0 = script name, $1–$9 = positional parameters, $# = number of arguments, $@ = all arguments, $? = exit status of last command (0=success), $$ = current PID.

Command substitution: result=$(command) or backticks. today=$(date +%Y-%m-%d).

Conditionals and Loops

if statement:

if [ condition ]; then ... elif [ condition ]; then ... else ... fi

Test operators: -eq (equal), -ne (not equal), -lt (less than), -gt (greater than), -f (file exists), -d (directory exists), -z (string empty), -n (string not empty), = (string equal).

for loop: for i in 1 2 3; do echo $i; done or for f in *.txt; do echo $f; done.

while loop: while [ condition ]; do ... done.

Functions:

greet() { echo "Hello $1"; } then call with greet Alice.

Exit codes: 0 = success. Non-zero = error. Use exit 0 or exit 1 to set the exit code. Check with echo $?.

Exam Tip

Shebang must be the first line: #!/bin/bash. Variables: no spaces around =. $? = last command exit status (0=success). [ ] requires spaces inside brackets. chmod +x makes a script executable.

Key Terms

Key Terms

Knowledge Check — 8 Questions

Linux Essentials — Shell Scripting

The shebang line #!/bin/bash must be:

  • The last line of the script
  • The first line of the script
  • A comment anywhere in the script
  • Optional
  • Answer: The first line of the script

The shebang must be the very first line — it tells the OS which interpreter to use to run the script. To make a script executable, use:

  • chmod 644 script.sh
  • chmod +x script.sh
  • chown +x script.sh
  • exec script.sh
  • Answer: chmod +x script.sh

chmod +x adds execute permission. Without it, ./script.sh gives “Permission denied”. $? contains:

  • The script name
  • The number of arguments
  • The exit status of the last command
  • The current PID
  • Answer: The exit status of the last command

$? = exit status. 0 = success. Non-zero = failure. Check immediately after a command to see if it succeeded.

In a bash script, name=”Alice” — what is wrong with name = “Alice”?

  • Nothing — both are correct
  • Spaces around = cause a syntax error
  • Double quotes are not allowed
  • The variable name is invalid

Answer: Spaces around = cause a syntax error
Bash variable assignment requires NO spaces around =. name = “Alice” is interpreted as running a command called “name”.

$1 in a script refers to:

  • The script name
  • The first argument passed to the script
  • The first line of the script
  • The PID
  • Answer: The first argument passed to the script

$1 = first positional parameter. $0 = script name. $# = number of arguments. $@ = all arguments.

Command substitution $(date) does what?

  • Runs date in the background
  • Captures the output of date into a variable or inline
  • Schedules date to run later
  • Displays the date command help

Answer: Captures the output of date into a variable or inline
$(command) runs the command and substitutes its output. today=$(date +%Y-%m-%d) stores the date in the variable today.

The -f test operator checks if:

  • A file is executable
  • A regular file exists
  • A directory exists
  • A file is empty
  • Answer: A regular file exists

[ -f file ] = true if file exists and is a regular file. [ -d dir ] = directory exists. [ -x file ] = file is executable.

A bash if statement ends with the keyword _____.

Answer: fi
if…then…fi — fi is “if” backwards. Similarly, case…esac and for/while…done.


Previous lesson: Archiving and compression: tar, gzip, bzip2, zip; package management commands
Back to Linux Essentials Preparation
Next lesson: Regular expressions: basic and extended regex, grep -E, sed, awk patterns