Creating Executable Bash Scripts
Full-Access Members Only
Sorry, this lesson is only available to Server Academy Full-Access members. Become a Full-Access member now and get instant access to this and many more premium courses. Click the button below and get instant access now.
Instructions
Q&A (0)
Notes (0)
Resources (0)
Saving Progress...
Resources
There are no resources for this lesson.
Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.
In this lesson, you will learn how to create executable bash scripts in Linux. Bash scripts are a powerful tool for automating tasks, running a series of commands, and managing system operations. By the end of this lesson, you will understand the basics of script creation, how to make a script executable, and how to run it. This skill is vital for anyone looking to streamline their workflow in a Linux environment.
Understanding Bash Scripts
A Bash script is a plain text file containing a series of commands that the bash shell can execute. These scripts can range from simple to complex, depending on the tasks they are designed to perform. The beauty of bash scripts lies in their ability to automate repetitive tasks, thereby saving time and reducing the potential for human error.
Creating a Basic Bash Script
To create a bash script, you first need a text editor. For beginners, nano
is a user-friendly choice. Let’s create a simple script that prints "Hello, World!" to the terminal. Open your terminal and type:
nano hello_world.sh
This command opens the nano
editor and creates a file named hello_world.sh
. In the editor, write the following script:
#!/bin/bash
echo "Hello, World!"
The first line, #!/bin/bash
, is called the shebang. It tells the system that this script should be run in the bash shell. The second line is the command that prints "Hello, World!" to the screen.
Making the Script Executable
After saving the file, the script is not yet ready to run. You need to make it executable. To do this, use the chmod
command:
chmod +x hello_world.sh
This command changes the script’s permissions, allowing it to be executed.
Running the Bash Script
Now that your script is executable, you can run it directly from the terminal. Navigate to the directory where your script is located and type:
./hello_world.sh
You should see "Hello, World!" printed in your terminal.
Verifying Your Script Works
To verify that your script runs correctly, you can look for the output in the terminal. After running the ./hello_world.sh
command, if you see "Hello, World!" displayed, your script is working correctly.
In the video above we also added a sub expression operator to the echo command like so:
echo "Hello, world! The current date is $(date)"
That's all we have to do in this lesson! See you in the next one..
Server Academy Members Only
Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.