Welcome to the most active Linux Forum on the web.
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 01-30-2012, 01:38 PM   #1
Registered: Mar 2006
Posts: 170

Rep: Reputation: 15
using cd in bash script

I want to write a script to change directory, and list the destination directory's contents. Basically an alias that does cd and then ls.

So I wrote a script:


cd $@

This does not have the expected behavior. When I run this script with my /media directory, it spits out the contents of the /media directory as requested, but when the script finishes, I am not cd'd to that directory. Apparently the shell forks a new shell when it runs the script and returns me to my current shell when it's finished.

How can I write a script to cd?

Also, I've noticed that I cannot do
which myscript | cd
Any insight as to why is appreciated.
Old 01-30-2012, 02:25 PM   #2
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
You got the first assessment correct.

The 2nd won't work because cd is a bash-built-in.

A quick work-around is to make your script a function.
function huh
  cd $@
To make this "permanent" just add it to your ~/.bashrc

Old 01-30-2012, 03:13 PM   #3
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Originally Posted by Daravon View Post
How can I write a script to cd?
You can't.

The only way you can do it is by running the script using the "source" command, which runs the script as if you typed the lines into the current shell.
Old 01-31-2012, 10:39 AM   #4
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
As discovered, the only way to get the directory to change in the current environment is to run the cd command in the current environment. A script can't do that. Only a typed command, sourced command, or function can.

As for why this doesn't work:
which myscript | cd
Pipes connect the stdout of command A to the stdin of command B. But cd isn't a command capable of reading from stdin. In addition, which outputs the path to the script with the filename attached. You'd have to strip it down to the path only before it could be used as input for cd.


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
[SOLVED] Run multiple bash and php scripts from a bash script charu Programming 5 07-26-2011 03:40 AM
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 09:37 AM
[SOLVED] Setting function default in a bash script - Bash sixtensixone Linux - General 13 01-08-2011 02:44 PM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 09:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:26 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