LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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-03-2013, 07:48 AM   #1
richa07
LQ Newbie
 
Registered: Jun 2013
Location: India
Posts: 8

Rep: Reputation: Disabled
Can we use cd command in shell scripting to go a directory


Can we use cd command in shell scripting to go a directory.
 
Old 07-03-2013, 08:19 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,545

Rep: Reputation: 922Reputation: 922Reputation: 922Reputation: 922Reputation: 922Reputation: 922Reputation: 922Reputation: 922
Yes
 
Old 07-03-2013, 08:41 AM   #3
mddnix
Member
 
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 525

Rep: Reputation: 141Reputation: 141
Quote:
Originally Posted by richa07 View Post
Can we use cd command in shell scripting to go a directory.
Yes. But there's a catch. You have to source it to change directory.

For example:
Code:
$ cat cdd
#!/bin/bash
cd /home/madhu/Documents

$ pwd
/home/madhu

$ ./cdd

$ pwd
/home/madhu

$ . cdd

$ pwd
/home/madhu/Documents
Notice the gap between . and cdd

Last edited by mddnix; 07-03-2013 at 08:42 AM.
 
Old 07-03-2013, 10:06 AM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,711

Rep: Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279
You need to understand that each process has a context for its operation - environment variables, working directory, open files, shared memory (and some others but these are the major ones).

The environment variables provided to a process are the "exported list" and are copied from the parent process. This allows new processes to inherit the exported list from the process that starts it. A process may change the values or add new names and values to the exported list. But a process cannot alter the values used by the parent process. The new values can only be used by new processes started (which is when they get copied) AFTER the new/modified values have been set.

Like environment variables, the working directory is also copied to new processes. Also like environment variables, a process cannot change the parents working directory - only its own and that will be inherited by any processes started after the change.

Open files can be a bit tricky. The only ones normally passed from process to process are stdin (file descriptor 0), stdout (file descriptor 1), and stderr (file descriptor 2). It is possible to create others though.

So a script running as a process can change its working directory. If the script is spawned as a new process (the usual case) then it gets a new context. If the parent process wants a script to change its working directory, then it must not spawn a new process - but instead include the script (the "source <filename>" thing) is used to redirect the command input to the <filename> until the <filename> finishes... NOTE: several peculiar looking things things can happen. If the script uses the "exit" command, then the process will terminate, and not go to the next command:
Code:
#!/bin/sh
# script for testing.sh
echo before xyz
source xyz.sh
echo after xyz
If the "xyz.sh" file contains:
Code:
#!/bin/sh
echo in xyz
Then you will get the result:
Code:
$./testing.sh
before xyz
in xyz
after xyz
But if the xyz file contains:
Code:
#!/bin/sh
echo in xyz
exit
Then you will get
Code:
$ ./testing.sh
before xyz
in xyz
$
Showing that the process that is running the "testing.sh" script terminated before reaching the end of the script.

Sometimes this is exactly what you want... But if you do it in a ".login" or "bash.rc" file (used during a login, or when you start a shell script) you will get logged out... or the shell will just exit without running your script.
 
Old 07-03-2013, 10:25 AM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Yes, but personally I prefer not to

Too easy to get 'lost' ( unless you store your pwd )
Instead I use pushd / popd
Code:
pushd /path/to/new/location
   ... Commands here ...
popd # returns to prior directory
You can do the same with
Code:
cd /path/to/new/location
   ... Commands here ...
cd $OLDPWD # returns to prior directory
But since pushd and popd use an array, you can nest them

Code:
pushd /path/to/new/location
   ... Commands here ...
     pushd /path/to/another/location
         ... Commands here ...
     popd
popd
Which you can't do with cd $OLDPWD

Note that all this cd'ing only applies to the script, once the script exits you will be where you invoked the script.

If you want to actually script a cd for your shell session, then an alias would do

Code:
alias Teleport="cd /path/to/new/location"
( or pushd if you want easy way to get back )

Alternatively, script your own bash 'function'

Code:
Teleport () {
cd /path/to/new/location
echo "You have teleported to /path/to/new/location"
}
( you would put those in your ~/.bashrc )
 
  


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
dos2unix command in shell scripting pdiv Linux - Server 3 07-05-2011 03:06 PM
[shell scripting] Small problem, for each file in directory... vraptor Linux - Newbie 11 05-04-2011 09:03 AM
shell scripting/ the fc -l command ! mayaabboud Linux - Newbie 12 01-03-2008 07:38 PM
shell scripting full directory listing sqn Programming 4 04-13-2005 05:33 PM
Does anyone know how to move files to a particular directory using shell scripting RowanB Programming 3 11-11-2004 02:46 PM

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

All times are GMT -5. The time now is 01:28 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration