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
beegee Assem
  • 107
  • 3
  • 12
  • You need to define the absolute path in `$PATH`, path to your script is missing a `/` at the beginning. – sebasth Sep 24 '17 at 13:08
  • I have added I just missed it here that's it I just checked my history – beegee Assem Sep 24 '17 at 13:11
  • Possible duplicate of [Problems when trying to execute sh file from another sh file](https://unix.stackexchange.com/questions/196135/problems-when-trying-to-execute-sh-file-from-another-sh-file) – G-Man Says 'Reinstate Monica' Sep 24 '17 at 13:53
  • Partial duplicate of [Problems when trying to execute sh file from another sh file](https://unix.stackexchange.com/q/196135/80216): don’t use `./xyz.sh` unless you’re in the directory where `xyz.sh` is located. – G-Man Says 'Reinstate Monica' Sep 24 '17 at 13:57

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,345
  • 1
  • 27
  • 29
djsmiley2kStaysInside
  • 1,326
  • 1
  • 10
  • 15
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
  • 320,670
  • 36
  • 633
  • 936