0

Under Mac OSX (Mountain Lion), I have a shell script "a":

#!/bin/bash
open -a Terminal b

which run another shell script "b" using Terminal:

echo `pwd`

Something interesting is that, no matter where my running scripts are located, the pwd command in "b" always returns the home directory.

Questions:

  1. Why does this happen?
  2. How to set the running environment to be the working instead home directory (ie, return the working directory when arriving at pwd)
Oli
  • 591
  • 4
  • 10
Hailiang Zhang
  • 17,604
  • 23
  • 71
  • 117
  • Sorry, don't you think your question is more appropriate for unix.stackexchange.com? – Paolo Aug 22 '13 at 22:18
  • possible duplicate of [Can a Bash script tell what directory it's stored in?](http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in) – Oli Aug 22 '13 at 22:27

2 Answers2

1

I am a ubuntu Linux user but I'm pretty sure the cli is very similar for both. Anyways, I believe the default directory when you open a new terminal window is your home directory. Unless you change it within your settings. This is the likely reason for the pwd command in "b" printing your home directory.

Bryan
  • 1,938
  • 13
  • 12
1

This will probably work in b:

echo $( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
Oli
  • 591
  • 4
  • 10