Writing my first shell script | Linux Fundamentals

Jesse Riddle
3 min readMay 11, 2020
Penguins on an iceberg.

CAUTION! Executing commands without review is dangerous. Operating systems have guardrails in place to maintain their operation. These guardrails will not protect you from deleting your own files. For example, the removal command cannot be undone. Be cautious. Maintain proper backups and beware of trolls on the internet.

You have gained the courage to open a Terminal running Bash on the Linux operating system. You forced yourself into learning basic Bash shell operations to navigate the Linux file system. You have even copied, pasted, and executed Bash commands that shared on the internet. All in all, you are well on your way to becoming the next 1337 H4X0R. The only thing left is:

A list of things you will need

  • A computer capable of executing Bash shell scripts.
  • Your favorite text editor

Preparing a project space

Let us create a project space for our your first Bash script. We will create a directory called Projects. Inside of it, we will create a directory called shell-scripts. These directories will be inside your $HOME directory.

Create the project space

mkdir -p "${HOME}/Projects/shell-scripts"

Navigate to the project space

cd "${HOME}/Projects/shell-scripts"

Creating the script

Let’s create an empty file in our project space. This file will contain the script that will write Hello World! to the screen.

touch ./hello-world.sh

Open hello-world.sh in your favorite editor. Add the following to the file and save it.

echo "Hello, World!"

Executing the script

Great, we have the script saved to $HOME/Projects/shell-scripts/. Let’s execute the script. From within the shell-scripts directory, execute the following…

bash ./hello-world.sh

If everything worked as expected, you should see.

Hello, World!

Woot, you have executed your first shell script! So what did we just do? Here we passed the script to Bash. Bash interprets the commands within our script and executes them.

Making it a tad better

You might have seen people executing scripts similar to the below.

./hello-world.sh

With a small update to the script, it will be able to do this as well.

Add A Shebang

Open hello-world.sh with your favorite editor. Update the script to the following…

#!/usr/bin/env bashecho "Hello, World!"

The line you added is called a shebang. This allows the operating system to know which interpreter to use for execution. This line directs the computer to use the version of bash that is defined for your local environment.

Modify File Permissions

Adding the shebang alone will not fix our problem. In Linux, the permissions of the file need to be updated so a script can execute. The command we need to do this is called chmod.

chmod +x ./hello-world.sh

Passing +x flag as an argument to chmod will update the file system so Linux knows the file is now executable.

Summary

Whew! We did more than execute a simple script. We also used several Linux commands to achieve the following:

  1. Create a home for future scripts
  2. Make a file executable
  3. Execute a simple script

We also learned how to use shebangs. Everything learned here helps build a solid foundation on which our skill-set can grow. Use this Bash script as a building block. Just add logic! Until next time, happy hacking!

--

--

Jesse Riddle

Minimalist | Maven | Disruptionist | Gastronome | Melophile