Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a simple script that I want to invoke 'pushed' followed by another command. But the 'pushd' command inside the script doesn't seem to stick past the script.

What's a way to make this execute in the shell terminal?

#!/bin/sh

pushd $1
time

What I really want to accomplish is invoke pushd followed by other-command with one command.

share|improve this question
    
Related question: unix.stackexchange.com/q/2976/250 –  phunehehe Dec 29 '10 at 0:49
add comment

2 Answers 2

Scripts cannot alter their parent processe's environment. Because of this, any environment changes made in the script are lost.

To run the script in the same process, you can 'source' the script like this

. /path/to/script.sh args
share|improve this answer
add comment

A shell script normally executes in a separate instance of the shell program, /bin/sh in this case. Your pushd command affects that sub-shell's working directory only. If it were otherwise, any program you ran from the shell could mess with your shell's working directory.

To execute that script within the current shell, say this instead:

$ . my-command somedir

or, more verbosely:

$ source my-command somedir

To make it appear that your program works like any other, you can use an alias:

$ alias mycmd='source my-command'
$ mycmd /bin
$ pwd
/bin
share|improve this answer
    
What I really want is execute 'pushd <params>' followed by another command that takes no params. I may not need a shell script for it - what's a good way to do this? –  Sarah Soto Dec 29 '10 at 0:23
    
@sarah-soto All you need then is pushd <params>; other-command –  Shawn J. Goff Dec 29 '10 at 0:26
    
I should have clarified: I want a shortcut so I am invoking both commands one after the after without implicitly calling both. I can't aliase the two commands to be alias p2='pushd; other-command' and then invoke 'p2 ~/temp' as the shell will return the error: '-bash: pushd: no other directory –  Sarah Soto Dec 29 '10 at 0:38
    
You could use a shell function: function p2 () { pushd "$@"; other-command;} –  Steven D Dec 29 '10 at 0:46
2  
A shell script is a fine way to do this. It's inefficient, but this is not a case where optimization is worth bothering with. Defining a shell function in your ~/.bash_profile (or whatever your shell uses, if not Bash) will also work. Your choice. –  Warren Young Dec 29 '10 at 3:10
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.