CD (change directory) - Not advisable for executing a command

Hi,

This is a quick one.
I have got a review comment in one of the scripts that i wrote:

"In UNIX script it not advisable to use cd to a directory and then run command."

Is this true?
I was trying to cd to log directory and use some cat or head or ls command.

Many Thanks,
Sam

Well, this is partially true. You may get confused when executing the script from another location or you may need to navigate back to the original folder.
You should rather use the full path to the file, i.e. 'cat /home/bob/test.txt'
Example :

cd /folder/test/
cat file1.txt
cd - 
# return to the last folder, variable kept in ENV as OLDPWD.
# or 
cd /home/bob/test/firstfolder/secondfolder/
cat somefile.txt

You can simply avoid all those 'cd' commands by just using the full path to the file.

another reason is you may be victim of spoofing (if . is in the PATH...) and other side effects (often with JAVA stuff...) like a program runs when you cd to the directory but fails when called from elsewhere (environment issue)...