3.3a Script Basics: Shebang, Variables, and Arguments
Key Takeaways
- A shell script is a text file of commands; the shebang line #!/bin/bash tells the kernel to run it with Bash
- Assign variables with name=value (no spaces around =); expand them with $name or ${name}
- Positional parameters $1, $2, … hold script arguments; $@ is all arguments; $# is the argument count
- echo prints text and expanded variables to standard output—the usual way to show messages from a script
- Quote expansions ("$1", "$@") when values may contain spaces; leave the shebang as the very first line of the file
Script Basics: Shebang, Variables, and Arguments
Objective 3.3 Turning Commands into a Script (weight 4) is the densest single sub-objective on the Linux Essentials blueprint. The idea is simple: anything you type repeatedly at the prompt can live in a text file and run as one unit. This section covers the foundation—shebang, variables, positional arguments, and echo—at Essentials depth (not LPIC-1 scripting depth).
What a shell script is
A shell script is a plain text file containing shell commands, one per line (plus comments and simple control structures you will meet next). When the shell runs the file, it executes those commands in order. Scripts save typing, reduce mistakes, and document a procedure you can share.
Create a file with any text editor (covered in the editors section), for example hello.sh:
#!/bin/bash
echo "Hello from a script"
The .sh extension is a helpful convention for humans; Linux does not require it. What matters is content, permissions, and how you invoke the file.
The shebang (#!/bin/bash)
The first line of many scripts is the shebang (also called a hashbang):
#!/bin/bash
| Piece | Meaning |
|---|---|
#! | Marks this line as the interpreter directive |
/bin/bash | Absolute path to the Bash interpreter |
When you run an executable script as ./hello.sh, the kernel reads the shebang and launches /bin/bash to interpret the rest of the file. For Linux Essentials, memorize #!/bin/bash as the standard Bash shebang. Other interpreters exist (#!/bin/sh, #!/usr/bin/python3), but this exam focuses on Bash basics.
Rules that trip candidates:
- The shebang must be the first line—no blank line or BOM before
#!. - Use the path
/bin/bashas taught for Essentials (do not invent exotic paths on fill-in items). - A leading
#elsewhere starts a comment; only#!at the start of the file is special.
If you run bash hello.sh, Bash itself reads the file and the shebang is optional for that invocation—but exams still expect you to know why the shebang exists and what it should say.
Variables in Bash
Scripts store reusable values in variables:
#!/bin/bash
COURSE="Linux Essentials"
YEAR=2026
echo "Studying $COURSE in $YEAR"
Assignment syntax is strict: name=value with no spaces around =. These are wrong:
COURSE = "Linux Essentials" # spaces break assignment
Expand (read) a variable with $name or ${name}. The brace form is clearer when the name sits next to other characters: ${COURSE}_notes.txt.
| Pattern | Example | Notes |
|---|---|---|
| Assign | DIR=/tmp | No $ on the left side |
| Expand | echo "$DIR" | $ on the right / in commands |
| Comment | # backup job | Ignored by the shell |
Environment variables you already use interactively (HOME, USER, PATH) are available inside scripts the same way: echo "Home is $HOME".
Positional arguments: $1, $@, $#
When you run a script with extra words after the name, those words become positional parameters:
./greet.sh Ada Lovelace
Inside greet.sh:
| Parameter | Meaning in this example |
|---|---|
$0 | Script name (./greet.sh or greet.sh) |
$1 | First argument (Ada) |
$2 | Second argument (Lovelace) |
$# | Number of arguments (2) |
$@ | All arguments (Ada Lovelace) |
Example script:
#!/bin/bash
echo "Script name: $0"
echo "Hello, $1"
echo "Argument count: $#"
echo "All args: $@"
Output for ./greet.sh Ada Lovelace:
Script name: ./greet.sh
Hello, Ada
Argument count: 2
All args: Ada Lovelace
$@ expands to each argument separately (especially important when quoted as "$@"). $# is an integer count—useful later for checking whether the user passed enough input. Essentials expects you to recognize $1, $@, and $# on sight; deeper arrays and shift are outside this level.
Always prefer "$1" and "$@" when expanding arguments so spaces inside a single argument stay intact. Unquoted $1 can split unexpectedly.
A common Essentials mix-up is treating $# as “the arguments themselves.” Remember the split: $1 / $2 are values, $@ is the whole list, and $# is only the count. If a fill-in asks how many arguments were passed, the token is $#, not $@.
Why quoting arguments matters (Essentials view)
Suppose a user runs:
./backup.sh "My Documents"
With proper quoting inside the script ("$1"), the directory name stays one value that includes a space. Without quotes, the shell can treat My and Documents as separate words and your script may back up the wrong path—or fail. You do not need advanced quoting theory for this exam, but you should default to double-quoting expansions you did not control.
Comments help future you (and graders reading sample scripts). A line starting with # after the shebang is ignored:
#!/bin/bash
# Purpose: print a friendly greeting using $1
echo "Hello, $1"
Keep comments short; they never replace a correct shebang or correct $ usage.
Using echo for output
echo writes its arguments to standard output, separated by spaces, then a newline:
echo "Backup starting"
echo "User=$USER host=$(hostname)"
Scripts use echo to print status messages, show expanded variables, and confirm which arguments were received. Options such as echo -n (no trailing newline) exist, but Essentials mainly cares that echo displays text and that variables expand inside double quotes.
Double quotes allow expansion: echo "Hello $1". Single quotes preserve literals: echo 'Hello $1' prints the characters $1 unchanged. For most script messages, double quotes are the right default.
Putting it together
#!/bin/bash
# greet.sh — Essentials-level demo
TARGET="$1"
if [ -z "$TARGET" ]; then
TARGET="world"
fi
echo "Hello, $TARGET"
echo "You passed $# argument(s): $@"
Even before you formalize if in the next section, notice the pattern: shebang first, assign from $1, then echo results. That is the core of “turning commands into a script.”
From interactive commands to a file
Anything useful at the prompt can become a script line. Interactively you might type:
echo "User is $USER"
echo "Home is $HOME"
The script version is the same two lines under a shebang. The win is reuse: run the file tomorrow without retyping. When you need a value that changes per run—an input directory, a username, a label—pass it as an argument and read it with $1 instead of hard-coding it.
Practice checklist for this section:
- Write
#!/bin/bashas line one. - Assign with
NAME=value(no spaces). - Print with
echo "$NAME". - Accept input via
$1/$@/$#. - Prefer
"$1"and"$@"in real use.
Exam focus
Expect fill-ins for #!/bin/bash, $1, $#, $@, and echo. Multiple-choice items often contrast “variable assignment needs $ on the left” (false) with correct NAME=value / $NAME expansion. Keep examples tiny and typed by hand until the tokens are automatic. If two options look similar, check for illegal spaces around = or a missing $ on expansion—those are the classic traps at Essentials level.
A Bash script begins with which shebang line for the standard Bash interpreter path emphasized on Linux Essentials?
You run ./report.sh east west north. Inside the script, what is the value of $#?
Which line correctly assigns a variable and then prints it with echo?