Jon
Jon

Reputation: 739

Swapping two files in shell

I'm trying to switch the contents of two files via temp file using the code;

#!/bin/bash

...

mv $FILE1 $TEMP
mv $FILE2 $FILE1
mv $TEMP $FILE2

When I run this, it returns for each line number

swap.sh: line 18: mv: command not found

Thanks in advance.

Upvotes: 0

Views: 314

Answers (2)

Olli
Olli

Reputation: 1245

Probably you are unsetting PATH variable with shell parameters.

Try adding full paths to commands, for example

/bin/mv ...

Using command which you can figure out what is correct path:

which mv

Alternatively you can try setting PATH at the beginning of script, for example:

PATH=/bin:/usr/bin:/sbin:/usr/sbin

This is system dependent. You can see the default PATH by running

echo $PATH

in your shell.

Upvotes: 4

Jens
Jens

Reputation: 72657

The POSIXy way to initialize your scripts with a proper PATH is

PATH=$(/usr/bin/getconf PATH)

Put this before the first command invokation and you should be set and protected from surprises by some random dude who has setup the shell profiles with his idea of what a PATH should look like.

You may wish to augment this, if you need programs from other directories, e.g.

PATH=$(/usr/bin/getconf PATH):/usr/local/bin

Upvotes: 0

Related Questions