I have some .bash_profile settings that restore my last working directory every time I log in to my shell. However, I also use a program that opens a terminal window in the current directory open in Finder, and it does this by sending an event to Terminal using SBApplication.

Is there a way I can detect which way the Terminal was opened in my .bash_profile? Right now the bash settings override the cd to app.

# Setting PATH for Python 3.4
# The orginal version is saved in .bash_profile.pysave
export PATH

# My settings
trap 'printf %s "$PWD" > ~/.storepwd' EXIT

cd "$(<~/.storepwd)"
  • Please edit your question adding the settings you've added to your .bash_profile that are relevant to this question. Feb 10, 2016 at 14:43
  • Please add an answer instead of editing a solution into your question. You might add it to the accepted answer or the user jherran can do it
    – klanomath
    Feb 11, 2016 at 17:59
  • BTW "PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin" ... export PATH" will probably break your PATH
    – klanomath
    Feb 11, 2016 at 18:03
  • @klanomath thanks, new to this site. Yes, I quickly edited out some stuff in my path that I didn't think was relevant to the question and forgot to leave the :${PATH} at the end :P
    – corym
    Feb 11, 2016 at 18:09

1 Answer 1


You can display BASH variables on your .bash_profile. Then compare the output when you manually open the terminal with output when cd to app opens the terminal.

I meant append at the beginning of your .bash_profile the bash variables listed on the website:

echo $BASH
echo $TMPDIR
echo $UID

Then open manually the terminal and save the output. Open the terminal from cd to and save the output.

Compare the files.

The proper variable for the above example is DIRSTACK.

Here are the new settings for those passing through:

# My settings
if [ $DIRSTACK = "~" ]; then
    trap 'printf %s "$PWD" > ~/.storepwd' EXIT
    cd "$(<~/.storepwd)"
  • 1
    Could you elaborate a bit on this, maybe include a working example too? Feb 9, 2016 at 14:16
  • BASHOPTS isn't set in the standard bash on OSX.
    – nohillside
    Feb 10, 2016 at 14:18
  • Thanks @jherran! The variable I was looking for is $DIRSTACK.
    – corym
    Feb 11, 2016 at 17:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .