We want to create a script (called zhead) that can be executed from any directory. To do this, we need to create a text file that will be the script. Second, we will add the script to a directory that is in the $PATH environment variable.

To make an example we will write a script that will display the first 10 lines of a gzipped file and call it zhead.

When we execute any command, we are just using a program, and a program is a file. For example, ls, is usually stored in /usr/bin/ls. So why we can use it in any directory?

When we type a command name, the shell looks for it in the directories listed in the PATH environment variable. The $PATH environment variable is a colon-separated list of directories that your shell searches when you type a command. If the command is found in one of the directories, the shell executes it. If the command is not found, the shell displays an error message.

To see the content of your PATH environment variable:

echo $PATH

You will see a list of directories separated by colons. These are the directories that the shell will search when looking for a command to execute.

:bulb: to see the same as a list, we can use sed to replace the colons with newlines:

echo $PATH | sed 's/:/\n/g'

1. Create the script

First, let’s create the script in your home directory:

nano ~/zhead

In the nano editor, enter the following content:

#!/bin/bash

if [ $# -eq 0 ]; then
    echo "Error: No file supplied"
    echo "Usage: zhead <gzipped-file>"
    exit 1
fi

gzip -d "$1" | head
  • :warning: the first line is the shebang line. It tells the system which interpreter to use to run the script. In this case, it specifies that the script should be run using the Bash shell. It must be the first line of the script, must start with #!, and must be followed by the path to the interpreter. No spaces are allowed between the #! and the interpreter path.
  • the if statement checks if the number of arguments passed to the script is equal to 0. If it is, the script prints an error message and exits.
  • the gzip command decompresses the gzipped file passed as an argument and pipes the output to the head command, which displays the first 10 lines of the file.

Press Ctrl+X, then Y, and Enter to save and exit.

2. Create the ~/bin directory

If it doesn’t already exist, create the ~/bin directory:

mkdir -p ~/bin

3. Move the script to ~/bin

Move the script to the ~/bin directory:

mv ~/zhead ~/bin/

If we try to execute the program now, we will get a permission denied error:

~/bin/zhead

This is because the file doesn’t have the executable permission set:

ls -l ~/bin/zhead

You should see output similar to this:

-rw-r--r-- 1 ubuntu ubuntu 123 Oct 19 10:00 /home/ubuntu/bin/zhead

The first characters show the [permissions of the file(https://www.redhat.com/en/blog/linux-file-permissions-explained). r means the file is readable, w means the file is writable, and x means the file is executable. The first character is a d if the file is a directory. There are no x characters in the permissions, so the file is not executable.

4. Make the script executable

Change the permissions to make the script executable (+x to add executable) for the file owner (u, for user):

chmod u+x ~/bin/zhead

Let’s verify the change:

ls -l ~/bin/zhead

You should see output similar to this:

-rwxr-xr-x 1 ubuntu ubuntu 123 Oct 19 10:00 /home/ubuntu/bin/zhead

The x in the permissions indicates that the file is now executable.

5. Add ~/bin to PATH

First, let’s check if ~/bin is already in your PATH:

echo $PATH

If you don’t see /home/ubuntu/bin or ~/bin in the output, we need to add it.

Edit your .bashrc file:

nano ~/.bashrc

Add the following line at the end of the file:

export PATH="$HOME/bin:$PATH"

Press Ctrl+X, then Y, and Enter to save and exit.

6. Refresh shell settings

To apply the changes without logging out and back in, run:

source ~/.bashrc

7. Verify the changes

Check your PATH again:

echo $PATH

You should now see /home/ubuntu/bin or ~/bin at the beginning of your PATH.

8. Test the script

Try running the script:

zhead

You should see the usage message:

Error: No file supplied
Usage: zhead <gzipped-file>

Now, try it with a gzipped file:

zhead /path/to/your/gzipped/file.gz

This should display the first 10 lines of the uncompressed file.

We did it!


Previous submodule:
Next submodule: