Run a script from anywhere

I have a script:

#!/bin/bash
echo "$(dirname $(readlink -e $1))/$(basename $1)"

that sits here: /home/myuser/bin/abspath.sh which has execute permissions.

If I run echo $PATH I get the following: /usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/myuser/bin

I wish to be able to, from any directory, call abspath <some_path_here> and it call my script. I am using bash, what I am doing wrong?


You want to type abspath, but the program is named abspath.sh. The problem is not regarding whether it is in the PATH, but the fact that you are simply not using its name to call it.

You have two options:

  1. Type abspath.sh instead.
  2. Rename the program to abspath.

This code is small enough that I would code it as a shell function:

abspath() {
    echo "$(dirname "$(readlink -e "$1")")/$(basename "$1")" 
} 

And yes you do want all those quotes.