9

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

3 Answers 3

20

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)

3
  • @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
    Commented Mar 2, 2019 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
    Commented Mar 2, 2019 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
    Commented Mar 2, 2019 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.

0

if "this is your full absolute path /Users/danylo.volokh/test/test_bash_script.sh" then if you are in Users then the exec path will be "danylo.volokh/test/test_bash_script.sh" else if you are in danylo.volokh then the exec path will be "test/test_bash_script.sh" else if you are in test then the exec path will be "./test_bash_script.sh" else if you do not know where in this world you are then the exec path will be "/Users/danylo.volokh/test/test_bash_script.sh"

this is for future users who need help with basic logic or if the op still doesnt have the answer (that would be embarrassing)

p.s this should work flawlessly in linux you might have to change it a bit for windows

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .