How to run a bash script via absolute path?

52,391

Solution 1

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)

Solution 2

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

Share:
52,391

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    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.

  • DavidPostill
    DavidPostill about 5 years
    @tatsu "It is absolutely unreal to me that executing sh scripts from absolute path is prevented" it is not prevented. Please read the answer carefully.
  • DavidPostill
    DavidPostill about 5 years
    @tatsu Why are you adding . to an absolute path? . refers to the current directory ... please read the answer carefully and the link in the answer as well.
  • tatsu
    tatsu about 5 years
    ok I read. from what I understand whithout the dot it should work. EDIT : yep it does work. this is just confusing. how can they expect us to guess this stuff?