Execute program with relative path and 'PATH' environment in Bash shell

relative path windows command line
batch file relative path up one level
relative path example
windows relative path up two levels
how to write a file path
folder path windows
how to write the relative path
bat resolve relative path

Bash Environment

Given a very simple disk structure as below

And environment path variable set to dir1 and dir2 as below

$ env|grep PATH
 returns :-
 PATH=/:/usr/bin:/e/path/to/directory/dir1:/e/path/to/directory/dir2

execution of the program fails as below

$ bin/prog.exe
bash: bin/prog1.exe: No such file or directory

or also

$ /bin/prog1.exe
bash: /bin/prog.exe: No such file or directory

however if we modify path to include /bin

PATH=/:/usr/bin:/e/path/to/directory/dir1/bin:/e/path/to/directory/dir2/bin

it does of course work

$ prog1.exe

Hello from prog1 ...

My question is how do I make paths relative to 'environment' PATH work in bash?

In practise I am given some files that have 10's of relative paths generated to many different virtual root locations, to which I cant change.

It is also not possible to use a full path, or just the executable name of (which we know works) for this scenario.

See man bash for explanation (emphasis mine):

If the name is neither a shell function nor a builtin, and contains no slashes, bash searches each element of the PATH for a directory containing an executable file by that name.

relative path in BAT script, Here is my own program folder on my USB drive: Program\ run.bat bin\ config.ini Iris.exe library.dll etc. I would like to use  Parentheses will create a new subshell to execute commands inside it. This new subshell will change directory and execute program in this directory. Quote from man bash (list) list is executed in a subshell environment (see COMMAND EXECUTION ENVIRONMENT below).

There's no such thing as relative path lookup. If your command name contains any / characters, it is treated as a path relative to your current working directory only. If it has no / characters, then the shell will look only in the exact directories, not any subdirectories under them, listed in your PATH.

Relative path lookups would raise a host of issues related to order in which subdirectories should be searched.

How to use relative paths on windows CMD?, Start a process using the file my_executable.exe found (with starting point in the current directory (relative path)) by going two directories back up and then down​  The program properly opened after changing the last parameter to show normally rather than just throwing "0". I also got it to run relative directory stuff by changing my program to run as an Administrator. It all works now, all I have to do is put it into a function:

As @choroba implies, you can't do what you're asking to do.

If you need to find a program in a subdirectory of one of the entries in your PATH, you'll have to iterate until you find it:

rel_path="bin/prog.exe"
IFS=: read -ra paths <<<"$PATH"
for path in "${paths[@]}"; do
    if [[ -x "$path/$rel_path" ]]; then
        exe="$path/$rel_path"
        break
    fi
done
if [[ -z "$exe" ]]; then
    echo "cannot find $rel_path"
else
    echo "found $rel_path as $exe"
fi

Executing a command with a relative path - Java queries, When a command is executed with a relative path, the runtime uses the PATH environment variable to find which executable to run. Therefore, any user who can change the PATH environment variable can cause the software to run a different, malicious executable. The Execute method is similar to a procedure call in many programming languages. Execute( Path ) Parameters. Path A string specifying the location of the .asp file to execute. The Path parameter may be for either an absolute or a relative path. If Path is absolute, it must map to an ASP script in the same application as the calling .asp file.

File path formats on Windows systems, \Program Files\Custom Utilities\StringFinder.exe, An absolute path from any current directory for D:\ from the command prompt before running  The path will be relative to the current working directory, which may be different from the parent directory of run.bat. – Ansgar Wiechers Feb 18 '13 at 18:40 add a comment |

PATH (variable), In that case, executing such a program requires specifying an absolute ( /home/​userjoe/bin/script.sh ) or relative path ( ./script.sh ) on the command line. When a​  powershell.exe .DeploySymbols.ps1. If we call the script from other directory: powershell.exe .Scripts\DeploySymbols.ps1. we’ll get “CommandNotFound” exception: “The term ‘.IndexSymbols.ps1’ is not recognized as the name of a cmdlet, function, script file, or operable program.

The Command Line and Filesystem – A Primer for Computational , The shell and other programs can find out the absolute path to your home directory via the environment variable $HOME ; try running echo $HOME to see the  Add Script Flexibility with Relative File Paths Aaron Rothstein · Feb 15, 2016 · 11 Comments Using relative file paths for your PowerShell script’s supporting files ensure it continues to work when the script and files get moved around.

Comments
  • Sorry image shows as directory but on the system it is dir1 and 2
  • correct image now
  • There is no real reason to add / to your path. There shouldn't be any executable programs at the root of the file system.
  • Removed the makefile tag that has nothing to do with the question.
  • Of course your all right - just having a brain freeze moment - many thanks
  • Of course your all right - just having a brain freeze moment - many thanks