LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 11-10-2007, 12:10 AM   #1
cdcshu
Member
 
Registered: Jul 2006
Location: Shanghai
Distribution: Fedora, OpenSUSE
Posts: 31

Rep: Reputation: 16
Script cd not work as expected


Hello,

I met another script problem:-) In script, I want "cd" execute as #cd <> does.

Demo script test:

++++++++++++++++++++++++++++++++
#!/bin/sh

cd /home/test
++++++++++++++++++++++++++++++++

[root@agent ~]# chmod +x test
[root@agent ~]# ./test
[root@agent ~]#

How to make it execute as the result of "cd /home/test" ?

Thanks in advance,

cdcshu
 
Old 11-10-2007, 04:38 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
Hi,

I don't think you can do that.

If you run a script, this script is executed in a new subshell. All that is done inside this subshell (cd /home/test for example) is 'lost' once the script stops and control is returned to the shell that you started the script from.

If you want something done in your current shell, you could make an alias or a function (both are explained in the bash manpage).

To give you an idea: alias cdt='cd /home/test' will create an alias that is called cdt. You can use this alias to cd to /home/test.

BTW: Do not use test as name for your script/program. There's a;ready a program called test present and one could get confused.

Hope this helps.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
sh script not executing as expected ryzor Linux - General 4 12-18-2006 09:03 AM
Unary Operator expected. Bash script Blackout_08 Programming 2 06-22-2006 02:21 PM
If your hardware does not work as expected! Keruskerfuerst Suse/Novell 1 11-06-2005 05:43 AM
-ne argument expected WHY doesnt this work VisionZ Linux - Newbie 8 03-27-2004 12:39 AM
Script Problem - Does not give expected results Jose Muņiz Linux - General 3 07-12-2003 10:10 PM


All times are GMT -5. The time now is 04:49 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration