views:

1127

answers:

6

Hi, everybody!

Is it possible to change current directory from a script?

I want to create a util for directory navigation in bash. I have created a test script that looks like the following:

#!/bin/bash
cd /home/artemb

When I execute the script from the bash shell the current directory doesn't change. Is it possible at all to change the current shell directory from a script?

Thanks,
Artem B.

+5  A: 

When you start your script a new process is created that only inherits your environment. When it ends it ends. Your current environment stays as it is. You can start your skript

. myscript.sh

The . will evaluate the script in the current environment so it might be altered

Norbert Hartl
+1 because you're right. Though I sincerely doubt he'll want to source a directory changing script each time he needs it. Moreover, `.sh` extensions are totally eww. Don't use them.
lhunath
Yes, usually it is better not to use it that way. Most of the time you are glad that your current environment does not suffer. But then I have scripts to do some setup tasks for me including changing to the right place and then I . them, too. Btw. .sh is of course a matter of personal style. I probably wouldn't use it while installing scripts system wide. But in my ~/bin I use them to know what is what :)
Norbert Hartl
A: 

With pushd the current directory is pushed on the directory stack and it is changed to the given directory, popd get the directory on top of the stack and changes then to it.

pushd ../new/dir > /dev/null
# do something in ../new/dir
popd > /dev/null
seb
+6  A: 

You need to convert your script to a shell function:

#!/bin/bash
#
# this script should not be run directly,
# instead you need to source it from your .bashrc,
# by adding this line:
#   . ~/bin/myprog.sh
#

function myprog() {
  A=$1
  B=$2
  echo "aaa ${A} bbb ${B} ccc"
  cd /proc
}

The reason is that each process has its own current directory, and when you execute a program from the shell it is run in a new process. The standard "cd", "pushd" and "popd" are builtin to the shell interpreter so that they affect the shell process.

By making your program a shell function, you are adding your own in-process command and then any directory change gets reflected in the shell process.

winden
A: 

In light of the unreadability and overcomplication of answers, i believe this is what the requestor should do

  1. add that script to the PATH
  2. run the script as . scriptname

The . (dot) will make sure the script is not run in a child shell.

+2  A: 

If you are using bash you can try alias:

into the .bashrc file add this line:

alias p='cd /home/serdar/my_new_folder/path/'

when you write "p" on the command line, it will change the directory.

huvelbaki
+1  A: 

Putting the above together, you can make an alias

alias your_cmd=". your_cmd"

if you don't want to write the leading "." each time you want to source your script to the shell environment, or if you simply don't want to remember that must be done for the script to work correctly.

MergerMan