Script file location and execute script

Share this post to your friends !

This entry is part 6 of 6 in the series Writing your first script

WriteBash - 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
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
script-file-location-and-execute-script 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/writebash.com. You can do the following steps in turn.

danie@linuxmint ~ $ mkdir /home/danie/writebash.com
danie@linuxmint ~ $ mv hello_world /home/danie/writebash.com/
danie@linuxmint ~ $ export PATH=~/writebash.com:"$PATH"

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

danie@linuxmint ~ $ echo $PATH
/home/danie/writebash.com:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

Execute script file

Now, the /home/danie/writebash.com 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.

Continue reading the series«« Previous part: Assign executation permission for a bash script file
If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.


Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.