How to run a bash script via absolute path?

I have a file:

/Users/danylo.volokh/test/test_bash_script.sh

Content is very simple:

#!/usr/bin/env bash
echo "-- print from script"

I'm in folder "danylo.volokh"

This command runs fine:

Danilos-MacBook-Pro:~ danylo.volokh$ test/test_bash_script.sh 
-- print from script

But if I try to run in with absolute path I get an error:

Danilos-MacBook-Pro:~ danylo.volokh$ /test/test_bash_script.sh 
-bash: /test/test_bash_script.sh: No such file or directory

I want to run a command with absolute path from any folder and get the script to be executed.


I want to run a command with absolute path from any folder and get the script to be executed.

If I try to run in with absolute path I get an error:

/test/test_bash_script.sh 
-bash: /test/test_bash_script.sh: No such file or directory

File /test/test_bash_script.sh does not exist, and so cannot be executed.

  • An absolute path is defined as the specifying the location of a file or directory from the root directory (/).

  • /test cannot be an absolute path as the directory /test does not exist (it is a subdirectory of your home directory).

You have two choices:

  1. Use the correct absolute path to the script:

    /Users/danylo.volokh/test/test_bash_script.sh
    
  2. Use the path based on your home directory:

    ~/test/test_bash_script.sh
    

What is an absolute path?

An absolute path is defined as the specifying the location of a file or directory from the root directory (/).

Source Absolute path vs relative path in Linux/Unix

Since slashes always separate name components, if a pathname starts with a slash, the nameless "ROOT" directory is assumed to begin the pathname. The ROOT directory has no name. It is the root of the entire Unix file system tree.

A pathname starting with a slash is called an absolute pathname, since it always starts at the ROOT.

Because it is difficult to talk about a directory that has no name, we usually (incorrectly) use the name "/" (slash) for the ROOT directory. This is wrong, because name components of a pathname can’t contain slashes and slashes separate name components. Understand that when we use "/" for ROOT, we really mean "the nameless ROOT directory that is to the left of the slash", not the slash itself.

Source Unix/Linux Pathnames (absolute, relative, dot, dot dot)


The absolute path is /Users/danylo.volokh/test/test_bash_script.sh, not /test/test_bash_script.sh. Bash is right then.