Linux – “cd” in linux shell script returns “command not found!”

linuxshell

I have written a shell script called "a.sh" in linux with one line:

cd ..

: and then I run this with:

chmod +x a.sh
sh ./a.sh

: how do I run this without getting :

"Command not found" or "Can't cd". Maybe I have been looking at this code too long or am I doing something obviously wrong??

Note: I have since found out what was wrong. Emacs was inserting some strange "^m" character at the end of every line

Best Answer

cd is a builtin to your shell. Anything like /usr/bin/cd or /bin/cd just there for weird magical reasons.

First run this.

which sh

This will output the path to your sh executable

Try adding a shebang to your script. So the entire file looks like this

#!/bin/sh
cd ..

If that doesn't work then clarify the steps you've taken as Stefan has asked. As well as paste the exact error message.