Table of Contents
What “your first script” actually is
A shell script is just a text file that contains commands you could type in the terminal, written in order, and made executable so the shell can run them as a program.
In this chapter you will:
- Create a simple Bash script file
- Add the “shebang” line
- Run the script in a safe, explicit way
- Make the script executable
- Add a few simple interactive features (arguments and input)
Later chapters will cover variables, conditionals, loops, and functions in detail. Here you only need enough to get a working, practical “first script.”
Preparing the environment
You need:
- A terminal
- A text editor available in the terminal (for beginners,
nanois fine) - A place in your home directory where you can experiment, for example
~/scripts
Create that directory:
mkdir -p ~/scripts
cd ~/scripts
All the examples in this chapter assume you are inside ~/scripts.
Creating your first script file
Start by creating a file named hello.sh:
nano hello.sh
Inside nano, type:
#!/usr/bin/env bash
echo "Hello, world!"Then save and exit:
- In
nano: pressCtrl+O(write out),Enterto confirm, thenCtrl+Xto exit.
You now have a script file. It won’t yet run as a program until you execute it explicitly or make it executable.
The shebang: telling the system which shell to use
The first line:
#!/usr/bin/env bashis called the shebang. It tells the system which interpreter should run this file.
#!is the special marker/usr/bin/env bashfinds and runs thebashinterpreter in a portable way
Why it matters:
- Without a shebang, some shells might still run the script, but you can’t rely on which shell is used.
- With a shebang, your script behaves consistently, regardless of the user’s default shell.
From now on, assume your simple scripts will start with that line.
Running a script without changing permissions
Before making the script executable, you can run it by passing it directly to Bash:
bash hello.shYou should see:
Hello, world!
This works because you’re explicitly telling bash to read the file. The file itself doesn’t need execute permissions for this.
This is a safe way to test new scripts, especially ones you’re not sure about yet.
Making the script executable
To run the script more like a normal program (./hello.sh), you need to set the executable bit:
chmod +x hello.shYou can verify the permissions:
ls -l hello.shYou’ll see something like:
-rwxr-xr-x 1 youruser yourgroup ... hello.sh
The x characters indicate that the file is executable.
Now you can run it with:
./hello.sh
The ./ means “run the hello.sh file located in the current directory.”
If you forget ./ and just type hello.sh, the shell usually won’t find it, because the current directory is not normally in your PATH (this is a security feature).
Adding a bit more: messages and blank lines
Extend your script to print a couple of lines:
Open hello.sh again:
nano hello.shUpdate it:
#!/usr/bin/env bash
echo "Hello, world!"
echo "This is my first shell script."
echo
echo "It ran successfully."Notes:
- Each
echoprints one line. echowith no arguments prints a blank line.- Commands run from top to bottom.
Save and run:
./hello.shUsing arguments in your first script
Scripts can receive arguments from the command line. These are extra words you type after the script name.
In Bash, the first argument is $1, the second is $2, and so on. $0 is the script’s own name.
Edit hello.sh to make it greet a name if one is provided:
nano hello.shReplace the contents with:
#!/usr/bin/env bash
echo "Script name: $0"
echo "Hello, world!"
echo "You passed $# argument(s)."
echo "First argument is: $1"Save and run it with and without arguments:
./hello.sh
./hello.sh Alice
./hello.sh Alice BobYou’ll see:
$#is the number of arguments$1is the first one (if any)
At this stage, you’re just observing how arguments show up in a script. Later chapters will show how to check and validate them with conditionals.
Reading user input (basic `read`)
You can also ask the user for input while the script is running, using read.
Create a new script called ask_name.sh:
nano ask_name.shType:
#!/usr/bin/env bash
echo -n "What is your name? "
read name
echo "Nice to meet you, $name!"Explanation of the new pieces:
echo -nprints text without a newline, so the cursor stays on the same line as the question.read namewaits for the user to type a line and press Enter, and stores the text in a variable calledname.$namelater expands to whatever the user typed.
Make it executable and run it:
chmod +x ask_name.sh
./ask_name.shYou should see something like:
What is your name? John
Nice to meet you, John!
You’ll learn more about variables in the next chapter; here you just need to see how read and a simple variable can make your script interactive.
Using comments in your script
Comments help you (and others) remember what the script does. In Bash scripts, anything after # on a line is a comment, except the shebang line.
Update ask_name.sh:
#!/usr/bin/env bash
# Ask the user for their name and greet them.
echo -n "What is your name? " # Prompt printed without a newline
read name # Read one line of input into the variable 'name'
echo "Nice to meet you, $name!"Comments are especially useful while you’re learning, and when scripts get longer.
A small, practical first script: a quick backup
As a slightly more useful example, create a simple backup script that copies a file to a new name with a .bak suffix.
Create backup_file.sh:
nano backup_file.shAdd:
#!/usr/bin/env bash
# Very simple backup script.
# Usage: ./backup_file.sh filename
echo "Backing up file: $1"
cp "$1" "$1.bak"
echo "Created backup: $1.bak"Make it executable:
chmod +x backup_file.shTry it on a test file:
echo "Some data" > notes.txt
./backup_file.sh notes.txt
ls
You should see both notes.txt and notes.txt.bak. You’ll learn proper error checking and argument validation later; for now, this demonstrates that a short script can already do something repetitive for you.
Good habits for your first scripts
As you start writing scripts, try to follow these habits:
- Always include a shebang at the top.
- Use a dedicated directory (like
~/scripts) for your practice scripts. - Test with
bash scriptname.shfirst if you are unsure, then make it executable. - Use comments to document what each part does.
- Name scripts descriptively, e.g.
backup_file.sh,cleanup_tmp.sh.
These habits will make it much easier to expand your skills as upcoming chapters introduce variables, conditionals, loops, and functions.