Kahibaro
Discord Login Register

Writing your first script

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:

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:

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:

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 bash

is called the shebang. It tells the system which interpreter should run this file.

Why it matters:

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

You 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.sh

You can verify the permissions:

ls -l hello.sh

You’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.sh

Update it:

#!/usr/bin/env bash
echo "Hello, world!"
echo "This is my first shell script."
echo
echo "It ran successfully."

Notes:

Save and run:

./hello.sh

Using 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.sh

Replace 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 Bob

You’ll see:

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

Type:

#!/usr/bin/env bash
echo -n "What is your name? "
read name
echo "Nice to meet you, $name!"

Explanation of the new pieces:

Make it executable and run it:

chmod +x ask_name.sh
./ask_name.sh

You 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.sh

Add:

#!/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.sh

Try 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:

These habits will make it much easier to expand your skills as upcoming chapters introduce variables, conditionals, loops, and functions.

Views: 20

Comments

Please login to add a comment.

Don't have an account? Register now!