MacOS – How to differentiate between Terminal scripted launch and manual launch for .bash_profile settings

bashmacosterminal

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
PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin"
export PATH

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

cd "$(<~/.storepwd)"

Best Answer

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 $BASHOPTS
echo $BASHPID
...
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)"
fi