LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   cd in shell script (https://www.linuxquestions.org/questions/linux-newbie-8/cd-in-shell-script-470958/)

vkmgeek 08-05-2006 03:31 AM

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..

druuna 08-05-2006 04:04 AM

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.

theYinYeti 08-05-2006 04:48 AM

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.

ErrorBound 08-05-2006 09:28 AM

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.