Create and run your first Bash shell script

This is the start of a new series of tutorials on Free and Open Source Software. In this article, you will learn about bash scripting.

The series assumes that you are somewhat familiar with the Linux terminal. You don’t have to be a master, but knowing the basics will do just fine. I recommend going through the Terminal Basics series.

Linux command tutorials for absolute beginners

Never used Linux commands before? No thanks necessary. This tutorial series is for absolute beginners to the Linux terminal.

Who is this series for?

Anyone who wants to start learning bash shell scripting.

If you are a student and have shell scripting as part of your course curriculum, this series is for you.

If you’re a regular desktop Linux user, this series will help you make sense of most shell scripts you encounter while troubleshooting various programs and fixes. You can also use it to automate some common, repetitive tasks.

By the end of the Bash Basics series, you should be able to write simple to intermediate bash scripts.

All chapters of the series contain simple exercises that you can learn by doing.


You will learn bash shell scripting here. While there are other wrappers with mostly the same composition, their behavior still differs in a few points. Bash is the most popular and universal shell, hence start learning scripting with bash.

Your first script: Hello World!

Open Terminal. now Create a new directory To save all scripts you will create in this string:

mkdir bash_scripts

now Switch to this newly created directory:

cd bash_scripts

We Invited Create a new file here:


now, Edit the file and add echo Hello World her line. You can do this using the cat command’s append mode (using >):

[email protected]:~/bash_scripts$ cat > 
echo Hello World

I prefer adding newlines while using the cat command to add text.

Press Ctrl + C or Ctrl + D to exit the append mode of the cat command. Now if you check the contents of the script hellow_world.shyou should only see one line.

Create your first shell script

The moment of truth has arrived. I’ve created my first shell script. It’s time to Run the shell script.

do like this:


The echo command simply displays whatever is supplied to it. In this case, the Hello World shell script should output to the screen.

Run the first shell script

Congratulations! You have successfully run your first script. How cool!

Here’s a rerun of all the above commands for your reference.

Another way to run your scripts

Most of the time you will run shell scripts this way:


Which will result in an error because your file as a script does not have execute permission yet.

bash: ./ Permission denied

Add the execute permission for yourself to the script:

chmod u+x

Now, you can run it like this:

Run shell scripts

So, you’ve learned two ways to run a script. It’s time to focus on bash.

Convert your shell script to a bash script

Confused? In fact, there are many shells available in Linux. Bash, ksh, csh, zsh, and many more. Of all, bash is the most popular and is installed by default in almost all distributions.

The shell is a translator. It accepts and manages Linux commands. While the syntax of most shells remains the same, their behavior may differ at certain points. For example, the processing of parentheses in conditional logic.

This is why it is important to tell the system which shell to use to interpret the script.

when you are using bash hello_world.shI used the bash compiler explicitly.

But when you run shell scripts this way:


The system will use whatever shell you’re currently using to run the script.

To avoid unwanted surprises due to handling different syntax, you must explicitly tell the system which shell script.

How is it done? Use shebang (#!). Normally, # is used for comments in shell scripts. However, if #! It is used as the first line of the program, and has a special purpose of telling the system which shell to use.

So, change the content of so that it looks like this:


echo Hello World

Now, you can run the shell script as normal knowing that the system will use the bash shell to run the script.

Run a bash shell script


If you feel uncomfortable editing script files in the terminal, as a desktop Linux user, you can use Gedit or other GUI text editors to write and run scripts in the terminal.

🏋️ Workout time

It’s time to practice what you’ve learned. Here are some basic exercises for this level:

  • Write a bash script that prints “Hello Everyone”
  • Write a bash script that displays the current working directory (hint: use the pwd command)
  • Write a shell script that prints your username in the following way: my name is XYZ (hint: use $USER)

Answers can be discussed in This custom topic in the community forum.

Exercise in Bash Basics Series #1: Create and run your first Bash shell script

If you’ve been following the Bash Basics as FOSS series, you can submit and discuss answers to the exercise at the end of the chapter: experienced members are encouraged to give feedback to new members. Do note that there can be more than one answer to a given problem.

recent practice uses $USER. This is a private variable that prints the username.

Which brings me to the topic of the next chapter in the Bash Basics series: Variables.

Look out for that next week.

Source link

Leave a Comment

Your email address will not be published. Required fields are marked *