Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the output from my script say "mv: command not found" but not when I run it on the shell directly?

Tags:

bash

shell

mv

I wrote a small script that should rename some files in a directory for me.

#!/bin/bash

a=1
for i in *.jpg ; do
        new=$(printf "%04d.jpg" "$a")
        mv "$i" "$new"
        let a=a+1
done

#end of file

After running the script is says this: "mv: command not found"

How come there are no error outputs when I run the code directly on the shell like this:

for i in *.jpg ; do new=$(printf "%04d.jpg" "$a") ; mv $i $new ; let a=a+1 ; done
like image 524
Simona Avatar asked Oct 20 '25 03:10

Simona


1 Answers

It probably is a matter of PATH setting. The /bin directory should be inside your $PATH

For debugging purposes, add

echo PATH is $PATH

at start of your script, and perhaps put #!/bin/bash -vx as your script's first line. See this, execve(2), bash(1).

As a workaround, replace

         mv "$i" "$new"

with

         /bin/mv "$i" "$new"

See mv(1)

like image 139
Basile Starynkevitch Avatar answered Oct 22 '25 20:10

Basile Starynkevitch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!