I've got a script in my ~/.bin directory that changes iTerm's background before starting an SSH session (as detailed here, script slightly modified from this version).

If I start a regular login session and run my command, sshb servername, it works fine! The background image loads and the SSH session starts.

However, if I set up a profile to start a session with that same command, it errors out and says it can't find sshb. If I make my command ~/.bin/sshb servername, it runs the command, but says it can't find the ImageMagick convert command used in that script.

It seems as though the command shell command doesn't know about my $PATH. I suppose I could figure out where convert lives and edit the script to use the full path, but that seems to be treating symptoms, not the problem. Is there a way to make the command shell load my environment variables before it runs that command?

  • what do you et if you echo $PATH in the terminal session and where do you set your path (the info should be in the question not on an external site that could change etc)
    – mmmmmm
    Nov 15 '13 at 0:14
  • If I echo $PATH in a normal terminal, I get /Users/lannyh/.bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/git/bin. If I make the Command Shell command echo $PATH, it prints the literal string $PATH and terminates the shell with a dialog notice about quickly-closing shells. I linked to the script above because the exact contents of the script are almost certainly not pertinent to my question; do I need to delete that parenthetical? Nov 15 '13 at 19:46
  • If echo $PATH returns $PATH there is most probably an error in one of the startup files for bash. Did you have a look at them already?
    – nohillside
    Nov 15 '13 at 20:37
  • No. There's not a problem with the .bash_profile or .bashrc because everything works fine with a login shell; the PATH is set correctly and everything's hunky-dory. It's only a problem with these iTerm command shells. Nov 15 '13 at 22:03

Environment variables like PATH and TERM are normally inherited from the shell. When you run the script directly, they are unset.

You can run the script as a child process of a login shell by setting the command to bash -lc "~/.bin/sshb servername".

  • I must confess that my understanding of this is still on shaky ground. If I open up a terminal window, I get the whole shell environment to run a command from. What is iTerm actually doing that runs the command without the whole shell environment? Is it possible for me to do the same thing myself, manually, somehow? Nov 15 '13 at 22:14
  • 1
    You can use exec -c cmd to replace the process of the shell with cmd and clear the environment. Normally when you open a shell, iTerm 2 executes login -fp username, which runs the default login shell as a child process. If for example you set the command executed in new shells to /usr/bin/emacs, emacs won't have bash or any other shell as a parent process.
    – Lri
    Nov 15 '13 at 22:46

You must log in to answer this question.

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