cd in shell script
Hi this is my script file
#vi test.sh cd /usr/include ls ////#over now i m running script.. #sh test.sh Then it is doing listing of dirs but cd command is not getting executed..I mean when script returns..It is still there where I had started.. |
Hi,
Works as designed. If you start a script, the code itself is executed in a new shell. In this (new) shell the cd and ls commands are executed. When the script is done, the (new) shell is closed again and you end up where you started the script. Hope this clears things up a bit. |
To have your script change your working directory, you have to source it instead of executing it. So you run "source ./script.sh" instead of "./script.sh". Depending on your shell, you may or not replace the word "source" with a simple dot: ". ./script.sh".
Yves. |
It's closing the window immediately after it's finished. If you want to see the output of ls, you could consider saving it to a file:
Code:
ls /usr/include > output.txt |
All times are GMT -5. The time now is 07:58 AM. |