We want to create a script (called zhead) that can be executed from any directory. To do this, we’ll first create a text file that contains the script, then add it to a directory included in the
$PATHenvironment variable.
In this example, we’ll write a script that displays the first 10 lines of a gzipped file and call it zhead.
When we execute any command, we’re actually running a program — and a program is just a file.
For instance, the ls command is usually located at /usr/bin/ls. So why can we use it from anywhere?
That’s because when we type a command, the shell searches for it in the directories listed in the $PATH environment variable.
This variable is a colon-separated list of directories that the shell scans to locate executables.
If it finds the command in one of those directories, it runs it; otherwise, it shows an error.
To see the content of your $PATH variable:
echo $PATH
You’ll see a list of directories separated by colons. These are the directories your shell searches for commands.
💡 To display them as a list, you can use sed to replace colons with newlines:
echo $PATH | sed 's/:/\n/g'
Let’s create the script in your home directory:
nano ~/zhead
In the nano editor, add the following content:
#!/bin/bash
# Check if a filename was supplied
if [ $# -eq 0 ]; then
echo "Error: No file supplied"
echo "Usage: zhead <gzipped-file>"
exit 1
fi
gzip -dc "$1" | head
#!) — must be the first line of the script, with no spaces after #!.
It tells the system which interpreter to use. Here, /bin/bash specifies Bash.if statement checks whether an argument was provided; if not, it shows an error and exits.gzip command decompresses the gzipped file and pipes the result to head, which displays the first 10 lines.Press Ctrl+X, then Y, and Enter to save and exit.
📦 There is a more complete version of the script on github
~/bin directoryWe’ll place the script in a personal bin directory inside your home folder - where you have write permissions.
(Remember: ~ is shorthand for your home directory.)
If it doesn’t exist already, create it:
mkdir -p ~/bin
~/bin
Move the script into the bin directory:
mv ~/zhead ~/bin/
If you try to execute it now:
~/bin/zhead
You’ll likely get a “Permission denied” error because the file isn’t executable yet.
Check its permissions:
ls -l ~/bin/zhead
Example output:
-rw-r--r-- 1 ubuntu ubuntu 123 Oct 19 10:00 /home/ubuntu/bin/zhead
The first part shows the file permissions:
r = readablew = writablex = executableThere’s no x in the example, so it’s not executable yet.
(If the first character is d, it means the entry is a directory.)
Add execute permissions for the user (the file’s owner):
chmod u+x ~/bin/zhead
Verify the change:
ls -l ~/bin/zhead
Example output:
-rwxr-xr-x 1 ubuntu ubuntu 123 Oct 19 10:00 /home/ubuntu/bin/zhead
Now the script has execute (x) permission.
~/bin to your $PATH
Check whether ~/bin is already part of your $PATH:
echo $PATH
If you don’t see /home/ubuntu/bin or ~/bin in the list, you’ll need to add it.
Edit your .bashrc file:
nano ~/.bashrc
Add this line at the end:
export PATH="$HOME"/bin:"$PATH"
Press Ctrl+X, then Y, and Enter to save and exit.
Apply the changes without logging out:
source ~/.bashrc
Check again:
echo $PATH
You should now see /home/ubuntu/bin (or ~/bin) at the start of your $PATH.
Try running it:
zhead
You should see:
Error: No file supplied
Usage: zhead <gzipped-file>
Now test it with a gzipped file:
zhead /path/to/your/gzipped/file.gz
This should display the first 10 lines of the uncompressed file.
✅ Success! You’ve created a script and made it globally accessible through your $PATH.