Bash – mv : not found when executed from script that modifies PATH variable

bashpathshell-scriptvariable

I have this in my script. But is returning an error. If I execute the command in the console works fine

./script.sh[16]: mv: not found [No such file or directory]

I want move some files what starts in T353 from one folder to another. REMOTE_PATH is /tmp/ and PATH is my working directory

cd $REMOTE_PATH
mv T353* $PATH #this is the line 16

If I make mv /tmp/T353* . works fine.

Best Answer

You seem to be using the variable PATH in your script. This happens to be the variable that the shell uses to look up executables. This means that if you change it to something else which is not a :-delimited list of paths, or to a path that does not contain the executables that the script uses, the shell may no longer find things like mv or other standard utilities.

It is preferable to use lowercase letters in shell script variables for this reason (it's a matter of taste1), or to at least be aware that there are variable that the shell uses for various things, and that you should avoid modifying these unless, of course, you'd like to alter the shell's behaviour.

The variables that Bash uses are listed under the heading "Shell Variables" in the Bash manual.

Incidentally, the cd on the line above does work. It's because it's a special utility built into to shell itself. The shell thus does not have to look at $PATH to figure out where it is.

Also incidentally, you say that $PATH is your working directory. The shell already stores the current working directory in $PWD.

As a side note, make a habit of double-quoting your variables. See "Security implications of forgetting to quote a variable in bash/POSIX shells".


1 Well, mostly, but the POSIX standard reserves the namespace of uppercase variables for environment variables (i.e. exported shell variables) used by the shell and by the collection of standard utilities. AFAIK, it doesn't disallow the use of uppercase characters in non-exported shell variables, but the whole purpose of defining namespaces is to avoid clashes. Since, to a script, environment variables and shell variables have the same semantics, it's better to just avoid uppercase variables altogether.

Related Question