LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   cd command doesn't work in my script (https://www.linuxquestions.org/questions/linux-newbie-8/cd-command-doesnt-work-in-my-script-4175469397/)

v3ct0r 07-12-2013 07:35 AM

cd command doesn't work in my script
 
I dont know why, I wrote a script yesterday about select directory, then I find no matter what option I gave, I just wouldn't change directory.
So I wrote a script today which contains only one line.
Code:

cd /home
and I just failed, with no error message. Any ideas?
Additional, I'm not at /home

acid_kewpie 07-12-2013 07:40 AM

It's about scope. when you run the script it invokes a new shell, and it's within that shell instances that you change directory. When you finish the script, that shell terminates, so nothing inside the shell persists to the parent it was executed from. If you want to run a list of commands in your current shell, you'd source the file. run ". myscript" and you'll change directory. the source dot there is broadly like an "include" statement in programming languages.

v3ct0r 07-12-2013 07:55 AM

Quote:

Originally Posted by acid_kewpie (Post 4989046)
It's about scope. when you run the script it invokes a new shell, and it's within that shell instances that you change directory. When you finish the script, that shell terminates, so nothing inside the shell persists to the parent it was executed from. If you want to run a list of commands in your current shell, you'd source the file. run ". myscript" and you'll change directory. the source dot there is broadly like an "include" statement in programming languages.

Then I find in a sub shell, pwd is still there, so Where can,I get to know the difference between bash and a sub shell.
I simply treat /bin/bash as a shell? Am I right?
Now I have some questions list here.
1:I invoke a sub ahell, where does this sub shell come from?
2:It's said that env variables is still supported in sub shell, why doesn't it have complete builtin command?
Thanks for your help.

Firerat 07-12-2013 08:01 AM

http://www.tldp.org/LDP/Bash-Beginners-Guide/html/

BUT, I notice an Andy in your OS info

are you trying to write scripts for Android?

v3ct0r 07-12-2013 08:10 AM

Quote:

Originally Posted by Firerat (Post 4989054)
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/

BUT, I notice an Andy in your OS info

are you trying to write scripts for Android?

Ha,Just use phone to post.
Guess I'm really attached to my phone.

druuna 07-12-2013 08:11 AM

Quote:

Originally Posted by xeechou (Post 4989052)
Then I find in a sub shell, pwd is still there, so Where can,I get to know the difference between bash and a sub shell.
I simply treat /bin/bash as a shell? Am I right?

Bash is a shell (BASH -> Bourne Again SHell). The term bash shell is a pleonasm.

Quote:

Originally Posted by xeechou
1:I invoke a sub ahell, where does this sub shell come from?

From the mother shell (the initial shell started when logging in). A mothewr shell can spawn child shells, which in turn can spawn child shells.

Quote:

Originally Posted by xeechou
2:It's said that env variables is still supported in sub shell, why doesn't it have complete builtin command?
Thanks for your help.

Maybe this might help clear things up a bit: Linux Shell environment


All times are GMT -5. The time now is 06:18 PM.