1

I have an Ubuntu Studio 16.10 64 bit. I created a shell script xyz.sh the path to the file is home/somefolder/test/xyz.sh I have already added

`chmod u+x xyz.sh` 

and I ran the script

./xyz.sh

It was successfull.

However, when I did a cd to go to my home directory and executed xyz.sh using

 ./xyz.sh 

I got the message bash: ./xyz.sh: No such file or directory

Despite executing the following:

PATH=$PATH:home/somefolder/test/xyz.sh

closed as off-topic by sebasth, Stephen Rauch, G-Man, Anthon, GAD3R Sep 24 '17 at 19:13

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – sebasth, Stephen Rauch, Anthon, GAD3R
If this question can be reworded to fit the rules in the help center, please edit the question.

4

If you've added the path correctly, you just need to call the name of the script, so xyz.sh rather than using ./xyz.sh

By using ./ you telling your shell to look in the current working directory, and run xyz.sh from there.

--

Side note, you're missing a / from the beginning of your directory path, it should be PATH=$PATH:/home/somefolder/test and you should only add the directory, not the entire executable name.

  • When an answer has worked for you, you'd normally accept it, by hitting the the big tick. – djsmiley2k Sep 24 '17 at 20:23
3

Explicitly stating a path to an executable will make the shell try to use that path to execute the executable.

If saying ./myscript and if myscript is not in the current directory, then you will get a "no such file or directory" error. This does not use the $PATH variable.

The $PATH should be a :-delimited list of directories (not files) in which the shell will search for executables when no path is specified on the command line. It is a potential security risk to add the current directory (.) to the PATH variable (see "Is it safe to add . to my PATH? How come?").

Another simple solution for when you just want to have access to a single executable outside of you ordinary $PATH is to use an alias:

alias myscript=/path/to/myscript

(this goes in your shell initialization file, probably .bashrc for bash).

You should specify the full absolute path to the executable in the alias.

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