6

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.

  • 3
    The absolute path is /Users/danylo.volokh/test/test_bash_script.sh not /test/test_bash_script.sh – slebetman Apr 20 '16 at 11:57
  • 3
    This question has a basic understanding problem. VTC? – cst1992 Apr 20 '16 at 12:30
  • I don't get it. you apparently know the full path of your script, yet you won't use it? – njzk2 Apr 20 '16 at 15:01
16

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)

  • @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 Mar 2 '19 at 16:31
  • @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. – DavidPostill Mar 2 '19 at 16:40
  • 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? – tatsu Mar 2 '19 at 16:43
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.