Hello! Welcome to a new module focused on a skill that truly defines a Linux power user: automation through shell scripting. In our previous lessons, we honed our manual troubleshooting skills with tools like ping and traceroute. Now, we'll begin learning how to make the system work for you by automating repetitive tasks.
This lesson kicks off our journey into shell scripting and directly addresses the learning outcome: Write basic bash scripts with proper shebang and execute permissions. We will cover the three fundamental steps to creating a functional script: writing the commands, specifying the correct interpreter with a "shebang," and setting the necessary permissions to run it. Mastering this foundation is essential for any Linux administrator and is a core skill for the CompTIA Linux+ exam.
1. What is a Bash Script?
At its heart, a bash script is simply a plain text file containing a sequence of commands. Instead of typing these commands one by one into the terminal, you can list them in a file and then instruct the shell to execute them all in order.
Why is this useful?
- Automation: It allows you to automate repetitive tasks, like backing up files, checking system status, or installing a standard set of software.
- Consistency: It ensures that a complex sequence of tasks is performed the same way every time, reducing the chance of human error.
- Efficiency: It saves you a significant amount of time by turning multi-step processes into a single command.
You already have experience with the command-line interface (CLI); think of a script as a way to save a CLI session and replay it on demand.
2. Writing and Running Your First Script
Creating a working script involves three key steps:
- Writing the script in a text editor and including a special first line called the shebang.
- Setting execute permissions on the file so the system recognizes it as a program that can be run.
- Executing the script from the command line.
To begin, let's get a solid overview of this entire process by reading from a classic guide on the Linux command line.
Writing your first script and getting it to work
This material from the 'Linux Command' project provides a clear, textual foundation for the three key steps involved in creating and running a script.
Please read the sections titled 'Writing a Script' and 'Setting Permissions'. Focus on understanding the purpose of the shebang, the role of comments, and how the chmod command is used.
3. A Guided Walkthrough: "Hello, World"
Now that you've read about the theory, let's put it into practice. The following video provides a clear, step-by-step demonstration of creating a "Hello, World" script, explaining each component as it goes.
How To Write Bash Scripts In Linux - Complete Guide (Part 2 - Hello World)
This comprehensive video from Learn Linux TV will walk you through the entire process of writing your first proper bash script, from the shebang to execution.
Please watch the following segments in order to see the full workflow: The Shebang (12:34 - 14:16): Understand why the #!/bin/bash line is the critical first line of any proper script. Putting it Together (14:16 - 16:31): See how the shebang is combined with commands like echo and pwd to create a complete script. Setting Permissions (06:50 - 08:35): Learn how to use the chmod command to make the script file executable and how to verify the change with ls -l. Running the Script (08:35 - 09:29): Discover how to execute the script from your current directory using the ./ syntax.
4. Consolidating the Concepts
Let's break down and solidify the three crucial concepts you just saw.
a) The Shebang: #!/bin/bash
This is the most important line in your script.

- The
#!at the beginning of the first line is the "shebang." It tells the operating system's program loader that this is a script to be interpreted. - The path that follows,
/bin/bash, is the absolute path to the interpreter that should run the commands in the file. - By explicitly stating
#!/bin/bash, you guarantee that your script will be executed by the Bash shell, even if the person running it uses a different default shell (likezshorfish). This makes your scripts portable and predictable.
b) Permissions: chmod +x
When you create a new text file, it has read and write permissions by default, but not execute permissions. You cannot run it as a program. As shown in the video, attempting to run it with ./myscript.sh will result in a "Permission denied" error.
To fix this, you must add the execute permission using the chmod (change mode) command:
chmod +x your_script_name.sh
+xmeans "add the execute permission" for the owner, group, and others.- You can verify the change with
ls -l your_script_name.sh. You will see anxin the permissions string, and the filename may change color.
While you saw chmod 755 in the reading, using chmod +x is often more intuitive for beginners, as it directly expresses the intent to "add execute." We will cover the numeric permission system in detail in a future module.
c) Execution: ./script.sh vs. bash script.sh
There are two common ways to run a script:
-
./your_script_name.sh
This is the standard way to run an executable file. The./is crucial. It provides a specific path (.for the current directory) to the file you want to run. Without it, the shell will only search for the command in the directories listed in the$PATHenvironment variable (like/bin,/usr/bin, etc.), and it won't find your script. This method requires the script to have execute permissions. -
bash your_script_name.sh
This method explicitly invokes thebashinterpreter and passes your script file to it as an argument. In this case, the script does not need to have execute permissions, because you are not executing the file directly.bashis the executable, and it is simply reading the commands from your file.
While the second method works, the first method (./script_name.sh) is the conventional and expected way to run a standalone script.
Test your understanding!
You've written a script named status.sh that contains the shebang #!/bin/bash. You try to run it with ./status.sh but get a "Permission denied" error. However, running it with bash status.sh works perfectly.
What is the single command you need to run to make ./status.sh work?
Show answer
You need to add execute permissions to the file. The command is:chmod +x status.sh
Conclusion
You have now learned the essential mechanics of creating and running a basic bash script. This process forms the foundation for all the automation you will do in Linux.
Key Takeaways:
- A shell script is a text file of commands used to automate tasks.
- Every script should begin with the shebang
#!/bin/bashto ensure it's interpreted correctly. - A script file must have execute permission to be run directly. You grant this using
chmod +x script_name. - The standard way to run an executable script in your current directory is with
./script_name.
Preview of the Next Lesson:
Now that you can create a simple, static script, the next step is to make your scripts more dynamic and powerful. In the next lesson, we will cover how to "Use variables, command substitution, and quoting in scripts." This will allow you to store and manipulate data, capture the output of other commands, and handle text with special characters, transforming your simple scripts into flexible tools.