No history yet

Shell Scripting Basics

Your First Shell Script

At its heart, a shell script is just a file containing a list of commands. The shell reads this file and runs the commands in order, one after another. It's like writing down a recipe for your computer to follow.

Let's create a simple script. Open a text editor and save the following text in a file named hello.sh.

#!/bin/bash
# This is a comment. The script starts below.

echo "Hello, World!"

The first line, #!/bin/bash, is called a shebang. It tells the system which interpreter to use to run the script. In this case, it's bash, the most common shell.

Before you can run the script, you need to give it permission to be executed. In the terminal, navigate to where you saved the file and use the chmod command, which stands for "change mode".

chmod +x hello.sh

This command adds (+) the executable (x) permission to your file. Now, you can run it.

./hello.sh

The ./ tells the shell to look for the script in the current directory. You should see "Hello, World!" printed to your screen.

Lesson image

Using Variables

Scripts become much more powerful when you use variables. Think of variables as labeled boxes where you can store information to use later. This makes scripts flexible and easier to read.

To create a variable, you just give it a name and assign it a value with the = sign. There should be no spaces around the equals sign. To use the variable's value, you put a dollar sign $ in front of its name.

#!/bin/bash

GREETING="Hello"
USER="Alice"

echo "$GREETING, $USER! Welcome."

Running this script would print "Hello, Alice! Welcome." Notice the variables are inside double quotes. This is important. Double quotes allow the shell to replace the variable name ($USER) with its value (Alice). If you used single quotes, it would print the literal text $GREETING, $USER!.

Making Decisions and Repeating Actions

Scripts can also make decisions using if statements. This lets your script check a condition and do something different based on the outcome.

The basic structure is if, then, and fi (which is if spelled backward). The condition goes inside square brackets [ ].

#!/bin/bash

if [ "$USER" == "root" ]; then
  echo "You are running as the root user."
else
  echo "You are running as a regular user."
fi

This script checks if the current user is root and prints a different message depending on the result. The else part is optional.

For repetitive tasks, you can use loops. A for loop is great for performing an action on a list of items.

#!/bin/bash

for FILENAME in file1.txt file2.txt file3.txt
do
  echo "Processing $FILENAME..."
  # You could add commands here to work with the file
done

This script will loop three times. In each loop, the FILENAME variable will hold the next name from the list, and the script will print a message. The loop starts with for, the actions are between do and done.

Connecting Commands

One of the most powerful features of the shell is the ability to connect simple tools to perform complex tasks. You can do this with redirection and pipes.

Redirection lets you send the output of a command somewhere other than the screen. The > symbol sends output to a file, overwriting anything that's already there.

# This command lists the contents of the current directory
# and saves that list in a file called 'directory_contents.txt'
ls -l > directory_contents.txt

If you want to add to the file instead of overwriting it, use two angle brackets: >>.

Pipes, represented by the | symbol, are even more interesting. A pipe takes the output from the command on its left and uses it as the input for the command on its right. It's like connecting a hose from one tool's output to another's input.

Imagine you want to find all the text files in a directory. You can list all files with ls, and then use grep to filter that list for lines containing .txt.

ls -l | grep ".txt"

Here, the full directory listing from ls -l is "piped" to grep, which then searches through it and only prints the lines that match. You've chained two simple commands together to do something more specific.

Ready to check your understanding?

Quiz Questions 1/6

What is the primary purpose of the line #!/bin/bash at the beginning of a shell script?

Quiz Questions 2/6

Which command is used to make a script named myscript.sh executable by its owner?

With these building blocks, you can start automating simple, repetitive tasks.