Simulate shell environment available to GUI Apps

applescriptapplicationsbashscriptterminal

I'm testing some scripts which are destined for use inside various GUI Apps, either as direct shell calls or as part of an AppleScript do shell script call. I know from experience that the shell which an App gets is nothing like what I get by default when starting Terminal, so I'd like to figure out a way to simulate the shell that an App gets so that I can debug the scripts knowing that they are behaving as I should expect them to behave in their final expected use environment.

So, how to I open an Terminal (or modify one which is already open) so that it behaves like the shell that a GUI App has access to?

Best Answer

First, let me solve the problem of a GUI app not providing the same environment as a terminal session to a bash script.

The differences are because of your bash profile, so you can have exactly the same in a GUI shell script by using the source command. Put the line source /Users/<username>/.bash_profile (replacing <username> with your short name) at the top of your bash script and you will have the same environment in the script when run inside a GUI app as in your shell session.

To get a shell without running your profile in iTerm2 (I couldn't get it to work in Terrminal):

  • Open to Terminal->Preferences
  • Go to the Profiles pane
  • Duplicate your default profile
  • In "General"
  • Change the name of the new profile to "No Profile"
  • Set the shortcut key to Control-Command-A
  • Click on "Command"
  • Set the command to /bin/bash -norc
  • Close the Preferences

Now Control-Command-A will open a session exactly the same as a GUI app would get - everything set to the bash default.