0

WARNING! Getting pretty basic here guys...

I have a rather elaborate shell script that I wish to create, but am haveing difficulty with the most basic of commands so am struggling to get going.

I want to create a .sh file that I can simply run using BASH - If I place just CD in the file and run in BASH nothing happens, whereas running CD from the terminal obv. gets me home.

Could someone shed some light on this please... I am running OSX

thanks

Pete Norris
  • 1,056
  • 7
  • 24
  • 36
  • 1
    possible duplicate of [How to run 'cd' in shell script and stay there after script finishes?](http://stackoverflow.com/questions/3879431/how-to-run-cd-in-shell-script-and-stay-there-after-script-finishes) – Mat Feb 23 '15 at 15:32
  • or http://stackoverflow.com/questions/255414/why-doesnt-cd-work-in-a-bash-shell-script?lq=1 – Mat Feb 23 '15 at 15:33
  • 1
    What do you mean 'nothing happens'? When you run the script, a sub-process is forked, the file is parsed, the subshell cd's and exits, returning a value to the calling shell. That's quite a bit of activity, and I left out many details! – William Pursell Feb 23 '15 at 15:35
  • Thanks, that link was the answer - and WIlliam Pursell... Like your answer :) – Pete Norris Feb 23 '15 at 15:41
  • Will be far better 1.) describe the problem what you want achieve 2.) what tried, 3.) input data and the wanted output (if acceptable) 4.) without the "excuse-me" sentences(!)... ;) :) – clt60 Feb 23 '15 at 15:42

1 Answers1

2

When you run a script you spawn a new sub-shell, your cd works in that and then you exit back to your orginal shell and your old present work directory - just as thought the cd never happened. If you want to short cut cds use a alias or a function. Something like:

go_dev() {
    cd /my/long/path/to/dev/env/
}

or

alias go_dev='cd /my/long/path/to/dev/env/'
Paul Evans
  • 27,315
  • 3
  • 37
  • 54