Linux··3 min

Shell Scripting Basics

Everything you have typed in the terminal so far has been a command you ran manually. Shell scripting is what happens when you stop running commands one at a time and start writing programs that run them for you.

Everything you have typed in the terminal so far has been a command you ran manually. Shell scripting is what happens when you stop running commands one at a time and start writing programs that run them for you.

A shell script is just a text file containing a list of commands. When you run it, the shell executes those commands in order. That is it.

Your first script

Open a new file with nano:

nano hello.sh

Type the following:

#!/bin/bash
echo "Hello, World!"

Save and exit. Now make the file executable:

chmod +x hello.sh

Run it:

./hello.sh

You will see Hello, World! printed to the terminal. You just wrote and ran a shell script.

Breaking it down

The first line #!/bin/bash is called a shebang. It tells the system which interpreter to use to run the script. /bin/bash is the path to bash, the most common shell on Linux. This line must always be the first line of your script.

echo prints text to the terminal. Whatever comes after it gets printed.

chmod +x adds the execute permission to the file. Without it, the system will not let you run the script.

./ before the filename tells the shell to look for the file in the current directory. Without it, the shell looks for a command with that name in the system's PATH and will not find your script.

Variables

Variables let you store values and reuse them.

#!/bin/bash
name="Luca"
echo "Hello, $name!"

No spaces around the = sign. To use a variable, prefix it with $.

User input

You can ask the user to type something with read:

#!/bin/bash
echo "What is your name?"
read name
echo "Hello, $name!"

Conditions

#!/bin/bash
echo "Enter a number:"
read number
 
if [ $number -gt 10 ]; then
  echo "Greater than 10"
else
  echo "10 or less"
fi

The comparison operators inside [ ] are:

  • -gt — greater than
  • -lt — less than
  • -eq — equal to
  • -ne — not equal to

Loops

Repeat something a set number of times:

#!/bin/bash
for i in 1 2 3 4 5; do
  echo "Number: $i"
done

Or loop while a condition is true:

#!/bin/bash
count=1
while [ $count -le 5 ]; do
  echo "Count: $count"
  count=$((count + 1))
done

A practical example

Here is a script that creates a backup of a folder by copying it with a timestamp in the name:

#!/bin/bash
source="/home/jan/documents"
timestamp=$(date +%Y-%m-%d)
backup="/home/jan/backups/documents-$timestamp"
 
cp -r $source $backup
echo "Backup created at $backup"

$(date +%Y-%m-%d) runs the date command and captures its output as a variable. This is called command substitution.

cp -r copies a folder and all its contents recursively.


Shell scripting is where Linux starts to feel genuinely powerful. Once you can write scripts, you can automate almost anything — backups, system maintenance, batch file processing, monitoring. The commands you have learned across this series are the building blocks. Scripts are what you build with them.

That wraps up this series on learning Linux from scratch. The foundation is there. Keep using the terminal, keep writing small scripts, and things will keep clicking into place.

1 views