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

5
  • 1
    possible duplicate of How to run 'cd' in shell script and stay there after script finishes? Commented Feb 23, 2015 at 15:32
  • or stackoverflow.com/questions/255414/… Commented Feb 23, 2015 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! Commented Feb 23, 2015 at 15:35
  • Thanks, that link was the answer - and WIlliam Pursell... Like your answer :) Commented Feb 23, 2015 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(!)... ;) :) Commented Feb 23, 2015 at 15:42

1 Answer 1

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/'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.