03. A few ways to execute source, ., bash

Setting permissions

To run our script, we must first set its permissions so that we can execute it. We can do this with the chmod command.

After our file mode is set to execute, we can then run the script with the source command.

# Set permissions so user can execute
$ chmod u+x hello_world
# Check to see that it's executable
$ ls hello_world
-rwxr--r--  1 JohnPC  staff  151 Jul  2 18:21 hello_world
# Execute our file
$ source hello_world
Hello world!

Great job! You just ran your first shell script!

Let's now go through the various ways you can execute your script. The two main methods are with a command and as a command.

Executing with a command

We can easily execute our script with three different commands. The first, which we already saw, is the source command. This takes as an argument the path name of our script, and executes it. A shortcut for source is the period (.). You may also use bash command if you are using a bash interpreter or sh for the regular shell.

$ source hello_world
Hello world!
$ . hello_world
Hello world!
$ bash hello_world
Hello world!
$ sh hello_world
Hello world!

All four commands perform the same task. This way of executing may seem convenient right now, but you can't execute it from anywhere on the file structure. Let's learn how to set up your system so you can execute as a command, which is more convenient in the long run.

Executing as a command

Oftentimes you'll want to execute your scripts as a command, just like every other command in the shell.

We can set this up in two ways:

  1. Place the script in a directory that's in our $PATH variable.
  2. Add the folder containing our script in the $PATH variable.

Recall that the $PATH variable contains all the folders the shell looks through when executing a command.

1) Adding script to a $PATH directory

To begin, let's use the echo $PATH to check out which folders are in our $PATH directory.

$ echo $PATH

You could technically add your script to any of your listed folders, but convention holds that we should list them in /usr/local/path. Now let's move our program to this folder.

$ mv hello_world /usr/local/bin
# Now the shell can access our program from anywhere on the file structure!
$ hello_world
Hello world!

2) Adding containing folder to $PATH

Another way to allow the shell to access our script is to add the containing folder to our $PATH. Add the following code to your ~/.bash_profile. This is the file that gets run every time you start a new shell. Setting this up has to do with shell and environment variables.

export PATH

Great! Now restart your terminal, or simply run the .bash_profile file.

$ source ~/.bash_profile
$ hello_world
Hello world!

Great job!

PHEW! Okay, so hopefully all those methods don't overwhelm you. Note that executing as a command is more work to setup, but less work every time you access the script. Thus, it's important that you set up your $PATH accordingly so that you can easily run the script from anywhere on your file structure.

Take your Linux skills to the next level!

Command Line Kung Fu

Take your Linux skills to the next level! Try Linux & UNIX

Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.

$ Check price
14.9914.99Amazon 4.5 logo(27+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources