LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu
User Name
Password
Ubuntu This forum is for the discussion of Ubuntu Linux.

Notices


Reply
  Search this Thread
Old 02-16-2009, 11:12 AM   #1
mrbutabara
Member
 
Registered: Jan 2007
Location: Stafford, VA
Distribution: Ubuntu
Posts: 32

Rep: Reputation: 15
How to use the "cd" command in a script


Hi I am trying to use the "cd" command in a shell script and am not having any luck. When i select an option in a if then statement, i want to exit the script and change directories.


#!/bin/bash
echo "Do you really want to quit?"
echo "1. Yes"
echo "2. No"
read option

if [ $option -eq 1 ] ; then
cd /home/user
else
bash /home/use/Linux/Linux_and_me
fi
 
Old 02-16-2009, 11:18 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Changing directory inside a script has no effect in the parent shell. It takes effect inside the script only.
 
Old 02-16-2009, 11:23 AM   #3
mrbutabara
Member
 
Registered: Jan 2007
Location: Stafford, VA
Distribution: Ubuntu
Posts: 32

Original Poster
Rep: Reputation: 15
I understand that. I was wondering if there is ANY way of doing this? is there a way to write another script that will assist me in completing the task. I want it so that when i exit the script it will change to the directory specified in the script i posted above
 
Old 02-16-2009, 01:41 PM   #4
rikijpn
Member
 
Registered: Jun 2007
Location: Japan
Distribution: Debian lenny, DSL, Solaris 10
Posts: 157

Rep: Reputation: 33
hey

Apparently, you can't do this "running" a script. As its enviroment is lost when returning to your shell.
But, not "running" it but just "reading/loading" it would be a good way to do what you want.
The variable PWD represents the presently working directory (where you last used cd to, you might try it by "echo $PWD" which will return your current directory). So instead of
Code:
"cd /somewhere"
You could,
Code:
"export PWD=/somewhere"
(replace that on the script).
And instead of running the script using
Code:
bash scriptname
You should use source,
Code:
source scriptname
This will load its enviroment(retain all the variables used in the script), so your current directory will change to the one selected in the script.

Last edited by rikijpn; 02-17-2009 at 12:50 PM.
 
Old 02-17-2009, 09:05 AM   #5
mrbutabara
Member
 
Registered: Jan 2007
Location: Stafford, VA
Distribution: Ubuntu
Posts: 32

Original Poster
Rep: Reputation: 15
thanks for the help. This is doing very close to what i need it to do and it will have to work. It was a lot closer than i had before thanks a lot man. : )
 
  


Reply



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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 07:42 AM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 02:32 PM
Bash Script: Problem running variable command containing "" Paasan Programming 2 01-21-2004 01:45 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu

All times are GMT -5. The time now is 12:54 AM.

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