Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 07-03-2013, 07:48 AM   #1
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
Senior Member
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,998

Rep: Reputation: 1246Reputation: 1246Reputation: 1246Reputation: 1246Reputation: 1246Reputation: 1246Reputation: 1246Reputation: 1246Reputation: 1246
Old 07-03-2013, 08:41 AM   #3
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 537

Rep: Reputation: 149Reputation: 149
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:
$ cat cdd
cd /home/madhu/Documents

$ pwd

$ ./cdd

$ pwd

$ . cdd

$ pwd
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
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,844

Rep: Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478
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:
# script for
echo before xyz
echo after xyz
If the "" file contains:
echo in xyz
Then you will get the result:
before xyz
in xyz
after xyz
But if the xyz file contains:
echo in xyz
Then you will get
$ ./
before xyz
in xyz
Showing that the process that is running the "" 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
Senior Member
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

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

Too easy to get 'lost' ( unless you store your pwd )
Instead I use pushd / popd
pushd /path/to/new/location
   ... Commands here ...
popd # returns to prior directory
You can do the same with
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

pushd /path/to/new/location
   ... Commands here ...
     pushd /path/to/another/location
         ... Commands here ...
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

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

Alternatively, script your own bash 'function'

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


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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:23 PM.

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