LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-12-2013, 07:35 AM   #1
v3ct0r
Member
 
Registered: Jun 2013
Location: China
Distribution: Archlinux
Posts: 67

Rep: Reputation: Disabled
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
 
Old 07-12-2013, 07:40 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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.
 
1 members found this post helpful.
Old 07-12-2013, 07:55 AM   #3
v3ct0r
Member
 
Registered: Jun 2013
Location: China
Distribution: Archlinux
Posts: 67

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by acid_kewpie View Post
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.
 
Old 07-12-2013, 08:01 AM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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?
 
Old 07-12-2013, 08:10 AM   #5
v3ct0r
Member
 
Registered: Jun 2013
Location: China
Distribution: Archlinux
Posts: 67

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
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.
 
Old 07-12-2013, 08:11 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by xeechou View Post
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
 
  


Reply

Tags
bash



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] -cd command of urxvt doesn't work cyker Linux - Newbie 1 08-14-2010 01:31 PM
why doesn't work this command? baby_ Linux - General 5 12-13-2009 07:16 AM
cd command doesn't work in script with more than two arguments in quotes vipinchauhan LinuxQuestions.org Member Intro 5 05-01-2009 11:20 PM
the ./ command doesn't work Thane Ubuntu 14 09-30-2006 04:22 PM
script runs fine from a command line, but doesn't work from cron? kleptophobiac Linux - Software 5 05-03-2004 04:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration