Creating Your First Script

LinuxBeginner
Practice Now

Introduction

Welcome to the world of automation!

Imagine you are a chef. Instead of cooking every dish by hand from memory every single time, you write down a Recipe. The kitchen staff can then follow this recipe to create the dish perfectly, again and again, without you needing to supervise every step.

In Linux, a script is your recipe. It is a plain text file containing commands that the shell can read and execute in order. This is one of the oldest and most important forms of automation in Unix and Linux systems.

Shell scripting became popular because administrators needed a lightweight way to automate repetitive tasks long before modern configuration-management tools existed. Even today, many production systems still rely on small shell scripts for setup, maintenance, and troubleshooting.

In this introductory lab, you will write your very first recipe. You will:

  1. Draft: Create the script file.
  2. Write: Add the instructions (commands).
  3. Authorize: Give the file permission to run.
  4. Execute: Run your new program.

Create a Script File

First, we need a blank piece of paper to write our recipe on. In Linux, a script is just a plain text file.

While Linux does not force you to use file extensions, it is a helpful habit to name shell scripts with .sh. This helps humans recognize the file's role quickly, even though Linux itself cares more about file contents and permissions than filename extensions.

This is an important Unix idea: a file is usually identified more by what it contains and what permissions it has than by its name. A script is not a special file type created by a graphical wizard. It is simply text that a program such as Bash can read line by line.

Let's create an empty file named hello.sh in your project directory.

Verify you are in the right place:

pwd

Now, use the touch command. This command is the digital equivalent of picking up a blank sheet of paper:

touch hello.sh

Check your desk to make sure the paper is there:

ls

You should see hello.sh waiting for your instructions.

Write the Script Content

Now we need to write the actual recipe. But first, we need a special header.

Think of a script like a letter. You need to address it to the right person. In Linux, the first line of a script tells the system who should read it. This line is called the Shebang (#!).

We want the Bash shell to read our script, so we address it to /bin/bash. We will also add a simple instruction: echo. This command tells the computer to "Say this out loud."

The shebang is one of the oldest and most practical Unix conventions. When you run a script as a program, the operating system reads the first line and uses it to decide which interpreter should process the file. An interpreter is a program that reads instructions and executes them, instead of running a precompiled binary directly.

Let's open our blank file using the nano editor:

nano hello.sh

Now, type these two lines exactly as they appear:

#!/bin/bash
echo "Hello LabEx"

Let's decode this:

  • #!/bin/bash: The "Address Label". It tells Linux: "Please send these instructions to the Bash program."
  • echo "Hello LabEx": The "Instruction". It tells Bash to print Hello LabEx on the screen.

Without a shebang, a script may still work in some cases when you explicitly run it with bash hello.sh, because you are manually choosing the interpreter. But when you run it as ./hello.sh, the shebang is the standard way to tell the system what should handle the file.

Save and Exit:

  1. Press Ctrl + O to save.
  2. Press Enter to confirm.
  3. Press Ctrl + X to exit.

Double-check your handwriting by reading the file with cat:

cat hello.sh

Make the Script Executable

You have written a recipe, but right now, it is just a piece of paper. Linux has a strict security policy: text files cannot run as programs unless you explicitly give them permission. This prevents you from accidentally running your grocery list as code!

We need to turn the "Key" to unlock execution. The command for changing permissions (modes) is chmod. The key we want to add is +x (plus eXecute).

This does not change the file contents at all. It changes the file's mode bits, which are part of the metadata Linux stores about every file. In other words, chmod +x is not making the script "more script-like"; it is simply telling the system that this file is allowed to be executed as a program.

Grant permission to your script:

chmod +x hello.sh

Let's check the file's ID badge again to see if it worked:

ls -l hello.sh

Look at the permissions on the left. You should see an x (like -rwxr-xr-x). The filename might also turn green, which is the terminal's way of saying: "Go ahead, this is safe to run."

Run the Script

The moment of truth. Let's cook the recipe!

To run a script that is sitting right in front of you (in your current directory), you need to be very specific. You cannot just type hello.sh. You must type ./hello.sh.

  • . means "Current Directory".
  • / is the separator.

So ./hello.sh literally means: "Run the file named hello.sh that is inside this folder."

This surprises many beginners. The reason is that your shell usually searches only the directories listed in the PATH variable when you type a command name by itself. For safety, the current directory is usually not searched automatically. That is why ./ matters: you are giving an explicit path instead of asking the shell to search for the command elsewhere.

Run your script:

./hello.sh

If everything went well, your computer should politely reply:

Hello LabEx

Congratulations! You are no longer just a Linux User; you are now a Linux Programmer. You have successfully created, authorized, and executed your first automation script.

Summary

Well done! You've just unlocked a major superpower.

Let's review the "Scripting Workflow" you learned:

  1. Draft: Use touch or an editor to create the file.
  2. Code: Add the #!/bin/bash header and your commands.
  3. Unlock: Use chmod +x so Linux allows it to run.
  4. Execute: Run it with ./scriptname.sh.

This might seem small, but every massive automated system setup, from web servers to supercomputers, starts with simple scripts just like this one.