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.

Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.

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.

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.

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.

The script is almost done — fill in the two blanks marked ___ using the # 👉 hints, then run it and check the Output panel matches.

Two blanks again: pick the right comparison operator, and the keyword that closes an if block. Then run it.

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.

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)).