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

2 Answers2

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.

njsg
  • 13,495
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.

Kusalananda
  • 333,661