Scripting
By the end of this lesson you'll be able to turn a list of terminal commands into a reusable bash script — with variables, input, decisions, loops, and functions — so the computer does the repetitive work for you.
Part of the free Cli course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn
1. The Shebang & Variables
A script is just a text file full of commands. The first line is the shebang — #!/bin/bash — which tells the system to run the file with the bash interpreter. After that, you store values in variables : write name=value with no spaces around the = , and read it back with $name . Use ${ name } braces when the variable name sits right next to other text.
Running a script
There are two ways to run a saved script. Either make it executable once with chmod +x and run it directly, or hand the file to bash each time:
Forgetting chmod +x is the classic "Permission denied" trip-up — see Common Errors below.
2. Command Substitution & Input
Command substitution — $(command) — runs a command and drops its output straight into your line. So today=$(date +%A) stores today's weekday in a variable. To get input from the person running the script , use read : it pauses, waits for them to type, and stores the result in a variable you name.
3. Positional Arguments
Scripts become powerful when you pass them arguments — the extra words you type after the script name. Bash hands them to you as $1 (first), $2 (second), and so on. $# is the count of arguments, $@ is all of them as a list, and $0 is the script's own name. This is how one script can act on whatever you give it.
4. Conditionals: if, test, [ ] and [[ ]]
Conditionals run code only when a test passes. The shape is if [ test ]; then ... fi — and you must close it with fi . Numbers use -gt -ge -lt -le -eq -ne ; strings use = and != ; files use checks like -f (exists). [ ] is the classic test command; [[ ]] is the modern bash version that's safer with strings. Watch the spaces — you need one inside the brackets.
5. Loops, Functions & Exit Codes
A for loop walks through a list; a while loop repeats as long as its test stays true. A function wraps commands under a name so you can reuse them — inside it, $1 is the first argument and local keeps a variable private. Finally, every command sets $? , its exit code : 0 means success and anything else means a failure.
🎯 Your Turn: complete the for-loop
The script is almost done — fill in the two blanks marked ___ using the # 👉 hints, then run it and check the Output panel matches.
🎯 Your Turn: complete the if-test
Two blanks again: pick the right comparison operator, and the keyword that closes an if block. Then run it.
Pro Tips
- 💡 Quote your variables: write "$file" , not $file . If the value has spaces, quoting keeps it as one piece instead of splitting it.
- 💡 Use local inside functions so a function's variables don't leak into the rest of the script — the same idea as let / const scope in JavaScript.
- 💡 $((...)) does maths: count=$((count + 1)) . Plain $( ) runs a command; the double parentheses do arithmetic.
- 💡 Fail loudly: start scripts with set -euo pipefail once you're comfortable — it stops the script on the first error instead of charging on.
Common Errors (and the fix)
- "command not found" after an assignment — you put spaces around = . name = "Alice" is wrong; write name="Alice" with no spaces.
- "syntax error: unexpected end of file" — you forgot to close a block. Every if needs fi , every for / while needs done , and every case branch needs ;; .
- "Permission denied" when running ./script.sh — the file isn't executable. Run chmod +x script.sh first, or just run it with bash script.sh .
- "too many arguments" / "unary operator expected" in a test — an unquoted variable that's empty or has spaces broke the [ ] test. Always quote it: [ "$x" -gt 0 ] .
- "[: missing `]'" — you left out the space inside the brackets. It must be [ "$x" = "y" ] , with spaces just inside both brackets.
📋 Quick Reference
Concept
Syntax
Notes
Shebang
#!/bin/bash
Must be line 1
Variable
name="value"
No spaces around =
Read it
$name / ${ name }
Braces near other text
Substitution
x=$(date)
Captures command output
Input
read -p "Q? " v
Stores typed text in v
Arguments
$1 $@ $#
First, all, count
If
if [ cond ]; then … fi
Close with fi
For loop
for x in list; do … done
Close with done
While loop
while [ cond ]; do … done
Repeats while true
Function
f() { … }
$1 = first arg
Arithmetic
n=$((n + 1))
Double parentheses
Exit code
$?
0 = success
Frequently Asked Questions
Mini-Challenge: file-counter report
No blanks this time — just a brief and an outline. Write a small, genuinely useful script that takes a folder name as an argument, guards against being called with none, and reports how many items it holds. Check your output against the example in the comments.
🎉 Lesson Complete!
- ✅ A script starts with the shebang #!/bin/bash ; make it runnable with chmod +x
- ✅ Variables use name=value (no spaces) and read back as $name / ${ name }
- ✅ $(...) captures command output; read gets input; $1 / $@ / $# are arguments
- ✅ Decide with if … fi and [ ] / [[ ]] ; repeat with for and while … done
- ✅ Package work in functions; check success with the exit code $?
- ✅ Next lesson: Process Management — list, background, and stop running programs with ps , kill , and jobs
Practice quiz
What is the shebang line #!/bin/bash for?
- It tells the system to run the file with bash
- It is a comment
- It exits the script
- It prints bash
Answer: It tells the system to run the file with bash. The shebang on line 1 tells the system which interpreter to use.
How do you correctly assign a variable in bash?
- name = value
- name=value
- set name value
- let name = value
Answer: name=value. Assignment must have no spaces around the = sign: name=value.
How do you read back the value of a variable named user?
- @user
- &user
- $user
- %user
Answer: $user. Put a $ in front of the name: $user or ${user}.
What does $(command) do?
- Comments out the command
- Skips the command
- Defines a function
- Runs the command and inserts its output
Answer: Runs the command and inserts its output. Command substitution runs the command and drops its output into the line.
What does $1 refer to inside a script?
- The first positional argument
- The script name
- The number of arguments
- The exit code
Answer: The first positional argument. $1 is the first argument; $0 is the script name.
Which keyword closes an if block in bash?
- end
- fi
- done
- endif
Answer: fi. An if block is closed with fi.
Which numeric test operator means greater-than-or-equal?
- -gt
- -eq
- -ge
- -le
Answer: -ge. -ge means >= ; -gt is strictly greater than.
Which keyword closes a for or while loop?
- fi
- stop
- loop
- done
Answer: done. for and while loops are closed with done.
What does $? hold after a command runs?
- The exit code of the last command
- The script name
- The first argument
- The current directory
Answer: The exit code of the last command. $? holds the exit code; 0 means success, non-zero means failure.
How do you do arithmetic like adding 1 to a counter in bash?
- count = count + 1
- $((count + 1))
- math(count+1)
- add count 1
Answer: $((count + 1)). $((...)) does arithmetic, e.g. count=$((count + 1)).
Continue this course
- Previous: Pipes and Redirection
- Next: Process Management