home directory added to path only in iTerm

1,070 views
Skip to first unread message

Max Rothman

unread,
Mar 29, 2017, 12:21:50 PM3/29/17
to iterm2-discuss
When using iTerm2 (build 3.0.15) on MacOS 10.12.4, the PATH environment variable ends with /Users/<my-username>. I've verified this is part of the initial value of PATH by printing it at the top of /etc/profile. This only occurs in iTerm, on the built-in Mac Terminal app, the path is identical except that it doesn't end in my home directory.

Anybody have any idea what's going on here? Where does the initial value of PATH come from, and can it be changed? Is this because of a setting in iTerm somewhere?

George Nachman

unread,
Mar 30, 2017, 12:42:43 AM3/30/17
to iterm2-...@googlegroups.com
Check that **Prefs > Profiles > General > Command** is set to Login Shell, otherwise the dotfiles may not be loaded the same way. It's possible that Terminal does something with the environment that I don't do—I don't modify the PWD in the environment that launchd set for iTerm2 when it was launched, and maybe Terminal does some sanitization? At least for me, that environment variable is not set. You can confirm by doing this:

1. Select the menu item iTerm2 > Toggle debug log
2. Create a new tab
3. iTerm2 > Toggle debug log
4. Check /tmp/debuglog.txt. The initial environment is logged. The relevant section of log looks like this:

1490848654.145231 PTYTask.m:481 (-[PTYTask launchWithPath:arguments:environment:width:height:isUTF8:]): Environment is
{
    COLORFGBG = "7;0";
    "ITERM_PROFILE" = Default;
    "ITERM_SESSION_ID" = "w2t1p0:57409868-E08E-4380-BBF9-72B856012E11";
    LANG = "en_US.UTF-8";
    PWD = "/Users/gnachman";
    SHELL = "/bin/tcsh";
    TERM = "xterm-256color";
    "TERM_PROGRAM" = "iTerm.app";
    "TERM_PROGRAM_VERSION" = "3.0.15";
    "TERM_SESSION_ID" = "w2t1p0:57409868-E08E-4380-BBF9-72B856012E11";
}

If you've got a PATH=/Users/your-username in there, that'd explain it. Let me know what you see.

On Wed, Mar 29, 2017 at 9:21 AM, Max Rothman <wheresw...@gmail.com> wrote:
When using iTerm2 (build 3.0.15) on MacOS 10.12.4, the PATH environment variable ends with /Users/<my-username>. I've verified this is part of the initial value of PATH by printing it at the top of /etc/profile. This only occurs in iTerm, on the built-in Mac Terminal app, the path is identical except that it doesn't end in my home directory.

Anybody have any idea what's going on here? Where does the initial value of PATH come from, and can it be changed? Is this because of a setting in iTerm somewhere?

--
You received this message because you are subscribed to the Google Groups "iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Max Rothman

unread,
Mar 30, 2017, 5:24:55 PM3/30/17
to iterm2-discuss, gnac...@llamas.org
I don't currently have iTerm launching the login shell, it's configured to use "/usr/local/bin/bash --login".

This is what I got from the debug log:
490908826.710143 PTYTask.m:481 (-[PTYTask launchWithPath:arguments:environment:width:height:isUTF8:]): Environment is
{
    COLORFGBG = "7;0";
    "ITERM_PROFILE" = Default;
    "ITERM_SESSION_ID" = "w0t1p0:7BB9904D-6F2C-4110-AAC7-B0996AD85DDB";
    LANG = "en_US.UTF-8";
    PWD = "/Users/max/config";
    SHELL = "/bin/bash";
    TERM = "xterm-256color";
    "TERM_PROGRAM" = "iTerm.app";
    "TERM_PROGRAM_VERSION" = "3.0.15";
    "TERM_SESSION_ID" = "w0t1p0:7BB9904D-6F2C-4110-AAC7-B0996AD85DDB";
}

So it doesn't appear the environment variable is coming from launchd. Curiouser and curiouser!

On Thursday, March 30, 2017 at 12:42:43 AM UTC-4, George Nachman wrote:
Check that **Prefs > Profiles > General > Command** is set to Login Shell, otherwise the dotfiles may not be loaded the same way. It's possible that Terminal does something with the environment that I don't do—I don't modify the PWD in the environment that launchd set for iTerm2 when it was launched, and maybe Terminal does some sanitization? At least for me, that environment variable is not set. You can confirm by doing this:

1. Select the menu item iTerm2 > Toggle debug log
2. Create a new tab
3. iTerm2 > Toggle debug log
4. Check /tmp/debuglog.txt. The initial environment is logged. The relevant section of log looks like this:

1490848654.145231 PTYTask.m:481 (-[PTYTask launchWithPath:arguments:environment:width:height:isUTF8:]): Environment is
{
    COLORFGBG = "7;0";
    "ITERM_PROFILE" = Default;
    "ITERM_SESSION_ID" = "w2t1p0:57409868-E08E-4380-BBF9-72B856012E11";
    LANG = "en_US.UTF-8";
    PWD = "/Users/gnachman";
    SHELL = "/bin/tcsh";
    TERM = "xterm-256color";
    "TERM_PROGRAM" = "iTerm.app";
    "TERM_PROGRAM_VERSION" = "3.0.15";
    "TERM_SESSION_ID" = "w2t1p0:57409868-E08E-4380-BBF9-72B856012E11";
}


If you've got a PATH=/Users/your-username in there, that'd explain it. Let me know what you see.
On Wed, Mar 29, 2017 at 9:21 AM, Max Rothman <wheresw...@gmail.com> wrote:
When using iTerm2 (build 3.0.15) on MacOS 10.12.4, the PATH environment variable ends with /Users/<my-username>. I've verified this is part of the initial value of PATH by printing it at the top of /etc/profile. This only occurs in iTerm, on the built-in Mac Terminal app, the path is identical except that it doesn't end in my home directory.

Anybody have any idea what's going on here? Where does the initial value of PATH come from, and can it be changed? Is this because of a setting in iTerm somewhere?

--
You received this message because you are subscribed to the Google Groups "iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discus...@googlegroups.com.

George Nachman

unread,
Mar 31, 2017, 4:44:42 PM3/31/17
to iterm2-...@googlegroups.com
bash --login is the most likely culprit. I gather you did that because you wanted to use bash for that shell but not make it your default shell? If you write a shell script like this it might accomplish that goal more successfully:

#!/bin/bash
export SHELL=/bin/bash
/usr/bin/login -fpl max /full/path/to/iTerm.app --launch_shell

This runs login, which then runs "iTerm2 --launch_shell" instead of your regular shell. When iTerm2 is run with --launch_shell it forks and execs your shell (based on $SHELL), inserting a - at the start of argv[0] so your shell thinks it's a login shell.


To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discuss+unsubscribe@googlegroups.com.

Max Rothman

unread,
Apr 1, 2017, 2:16:46 PM4/1/17
to iterm2-discuss, gnac...@llamas.org
Just for the heck of it, I changed my login shell and set iTerm to use it, but it didn't fix the issue, so it looks like the bash --login might not have been the problem?

George Nachman

unread,
Apr 9, 2017, 12:24:20 PM4/9/17
to iterm2-...@googlegroups.com
I would guess that a dotfile is somehow responsible then. Check .profile, .bash_profile, and .bashrc to see if the current directory is being changed somewhere in one of those files, perhaps indirectly.

To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discuss+unsubscribe@googlegroups.com.

Max Rothman

unread,
Apr 9, 2017, 2:47:17 PM4/9/17
to iterm2-discuss, gnac...@llamas.org
Current directory? What would that have to do with $PATH?

As I said in my original message, I've verified that my home directory is present in $PATH when /etc/profile runs, before any of my dotfiles. That initial path is "/usr/bin:/bin:/usr/sbin:/sbin:/Users/<my username>" when I run iTerm and "/usr/bin:/bin" when I run iTerm. Note that iTerm is configured to use the login shell, which is "/usr/local/bin/bash" (installed via homebrew).

Basically, something is appending "/usr/sbin:/sbin:/Users/<my username>" to $PATH before /etc/profile runs, which, according to when I run "bash --login -x", is nothing except iTerm itself*.



* Since I can only execute "bash --login -x" in an existent shell, thus letting that process inherit my environment variables, in theory something could be running before /etc/profile, but given that unless something fishy is going on, login shells always execute /etc/profile first, that seems unlikely.

George Nachman

unread,
Apr 14, 2017, 1:57:00 PM4/14/17
to iterm2-...@googlegroups.com
Sorry, I was in a hurry when I wrote that message. I looked at the source code for login and I don't think it could be responsible, so it's probably the shell, but let's make sure. Here's what I would do: change your shell to a program that prints $PATH. That'll tell you definitively whether it happens before or after the shell runs. Here's how:

Put this code in fakeshell.c:

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
    printf("%s\n", getenv("PATH"));
    return 0;
}

The build it like this:

cc fakeshell.c -o /tmp/fakeshell

Edit /etc/shells and add /tmp/fakeshell to it. Then do:

chsh -s /tmp/fakeshell

Create a new tab, and dismiss the yellow announcement and you'll see the $PATH your shell is launched with.

Make sure to keep another window around so you can change your shell back to bash, since fakeshell won't let you :)


To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discuss+unsubscribe@googlegroups.com.

Max Rothman

unread,
May 9, 2017, 8:56:41 PM5/9/17
to iterm2-...@googlegroups.com
I finally got back to playing with this, sorry for the delay! I tried out the compiled fakeshell, and it printed out a path with my home directory at the end. For good measure, I even tried changing my home directory to /tmp with dscl, and it still had /Users/<my-username> at the end.

If that's true with the fakeshell executable, that means it's coming straight from launchd, right? Since no files like /etc/profile could possibly be sourced?

--
You received this message because you are subscribed to a topic in the Google Groups "iterm2-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/iterm2-discuss/YfFACfqTz-o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to iterm2-discuss+unsubscribe@googlegroups.com.

George Nachman

unread,
May 13, 2017, 2:06:35 PM5/13/17
to iterm2-...@googlegroups.com
So we know your PATH is unset when we exec `/usr/bin/login -fpl max iTerm2 --launch_shell` and then it's set with your home directory in it after login exec's iTerm2 which exec's your shell. I'm confident iTerm2 isn't setting it (I just double checked the shell launcher code), so login has got to be responsible. Looking at the source for login (https://opensource.apple.com/source/system_cmds/system_cmds-735.50.6/login.tproj/login.c.auto.html) I don't see how it could happen, so just to confirm, run

export PATH=""
/usr/bin/login -fpl max /tmp/fakeshell

In that scenario, does fakeshell print a path with your home directory?


Max Rothman

unread,
May 13, 2017, 2:13:20 PM5/13/17
to iterm2-...@googlegroups.com
It prints a last login line and a blank line, which presumably means the path is empty.

George Nachman

unread,
May 13, 2017, 2:37:41 PM5/13/17
to iterm2-...@googlegroups.com
Well, that's interesting. I've added some extra code to a special build of iTerm2 to print the value of PATH at each step. Please install this build:


Set your shell to fakeshell as above and create a new tab. What do you see in the terminal window?

Max Rothman

unread,
May 13, 2017, 3:03:59 PM5/13/17
to iterm2-discuss, gnac...@llamas.org
Here's what I get:

PTYTask set PATH before exec to PATH=/usr/bin:/bin:/usr/sbin:/sbin:/Users/max
ExecChild: command=/usr/bin/login PATH=/usr/bin:/bin:/usr/sbin:/sbin:/Users/max
Last login: Sat May 13 14:59:59 on ttys001
shell_launcher: Exec'ing your shell /Users/max/fakeshell with PATH=/usr/bin:/bin:/usr/sbin:/sbin:/Users/max
/usr/bin:/bin:/usr/sbin:/sbin:/Users/max

--
You received this message because you are subscribed to a topic in the Google Groups "iterm2-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/iterm2-discuss/YfFACfqTz-o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to iterm2-discus...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discus...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "iterm2-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/iterm2-discuss/YfFACfqTz-o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to iterm2-discus...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

George Nachman

unread,
May 13, 2017, 3:59:11 PM5/13/17
to iterm2-...@googlegroups.com
So fakeshell was well-behaved in this build. I wonder if it's a difference between 3.0.x and 3.1. Has this problem already been fixed in 3.1.beta.3?

To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discuss+unsubscribe@googlegroups.com.

Max Rothman

unread,
May 13, 2017, 4:13:59 PM5/13/17
to iterm2-discuss, gnac...@llamas.org
What do you mean by "well-behaved"? My home directory was still in the path. If you're referring to when my path came out blank earlier, I suspect that was inherited form the parent process where we had exported it.

George Nachman

unread,
May 13, 2017, 5:46:32 PM5/13/17
to iterm2-...@googlegroups.com
Ah, yes, I misread it. I think you're right that launchd is to blame since it was present at the earliest point. I guess that Terminal.app probably does not pass a PATH to login. I'll file a task to change that but I'm wary of breaking things this late in the release cycle. For now you can work around it by setting the path to /usr/bin:bin:/usr/sbin:/sbin for login sessions in .bash_profile or .profile.

To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discuss+unsubscribe@googlegroups.com.

Max Rothman

unread,
May 13, 2017, 10:08:44 PM5/13/17
to iterm2-discuss, gnac...@llamas.org
I created the following applescript app, and it confirms the issue is with launchd:

display dialog "PATH: " & (system attribute "PATH")


FWIW, I don't think what iTerm currently does is necessarily wrong or should be changed. Now I just have to figure out why launchd has my home directory in the path! I can easily override it in my bashrc, but at this point I'm more curious than anything else.

I searched everything in /System/Library, /Library, and ~/Library for the string "/Users/max" and came up with blanks, so it's not something in a plist somewhere, but "sysctl user.cs_path" returns the correct path (without the home directory) and "launchctl getenv PATH" returns nothing. I also read about "sudo launchctl config (user|system) path" but I didn't see a way to read that info, so I was hesitant to set it.
Reply all
Reply to author
Forward
0 new messages