Script file location and execute script

by Daniel Pham
Published: Updated:
This entry is part 6 of 6 in the series Writing your first script

Now is the last step in 3 steps how to write a bash script file. This article will learn about the script file location and script execution. The execution of the script sounds very simple, but sometimes, you don’t understand why it doesn’t work as expected.

Script file location

Normally, when you write and assign the executable permissions to the script file, it is done. You can execute it with the following command (eg I use hello_world script file).

danie@linuxmint ~ $ ./hello_world

But if you do not provide the path of the script file, you will get the following error:

danie@linuxmint ~ $ hello_world
bash: hello_world: command not found

In fact, if you encounter the above situation, it’s not a problem for your script. It is simply that the system cannot know where your script is.

The system’s default PATH

By default, the Linux system contains executable libraries in the /bin (binaries) directory. So, when you execute the script without providing the path of the file. The system will automatically find in its default directory /bin and as a result it cannot find your script.

/bin is not the only directory in the system that contains executable files, there are other directories and may be declared by you.

The list of executable directories is in an environment variable of the system called PATH. To display the entire list of those directories, use the following command:

danie@linuxmint ~ $ echo $PATH
script file location and execute script
Displays the list of automatically searched folders of the system.

Now, you can put the script file into one of the above directories, if you want to put it in your directory such as /home/danie/ You can do the following steps in turn.

danie@linuxmint ~ $ mkdir /home/danie/
danie@linuxmint ~ $ mv hello_world /home/danie/
danie@linuxmint ~ $ export PATH=~/"$PATH"

Now let’s see if the PATH variable already contains your directory.

danie@linuxmint ~ $ echo $PATH

Execute script file

Now, the /home/danie/ folder is already in the list of system folders that will automatically search for executable files. Your script has also been placed in that directory, meaning that you can execute the script without calling the file path.

Simply calling the script name is like calling other common commands in the system.

danie@linuxmint ~ $ hello_world
Hello World

So you have all the basic steps to write and execute a bash script file. Practice a lot so you can feel comfortable using the script.

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating
Series Navigation«« Previous part: Assign executation permission for a bash script file

You may also like

Notify of
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.



Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 – All rights reserved.

Please write sources “” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Would love your thoughts, please comment.x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.