3

Using MSYS2, if I run msys2_shell.bat, mintty opens a bash login shell, but ~/.profile does not get sourced.

Anyway if I run /bin/bash --login inside mintty, ~/.profile get sourced. Why?

The same happens if I run path\to\msys64\bin\bash.exe --login via Windows prompt instead of msys2_shell.bat.

PS: I tried also with .bash_profile.

tinlyx
  • 18,900
  • 26
  • 81
  • 148
antonio
  • 9,285
  • 10
  • 59
  • 113
  • There is a difference between launching a shell with login and not. This is why you need the --login – xxcezz Jun 07 '14 at 16:19
  • the question is not about what a login shell is, but why `.profile` is not sourced. – antonio Jun 07 '14 at 16:32
  • Because you are not entering a 'login' shell.. This is a feature and the difference between a 'login' shell and not. – xxcezz Jun 07 '14 at 16:33
  • 1
    @xxcezz: Why did you say say that I am not in a login shell? `msys2_shell.bat` opens a login shell. – antonio Jun 07 '14 at 16:38
  • I misunderstood the issue. Sorry. Can you update msys2_shell.bat to create a login shell? – xxcezz Jun 07 '14 at 16:39
  • @xxcezz: Again? `msys2_shell.bat` does open a login shell! – antonio Jun 07 '14 at 16:44
  • Then I do not understand. You said that when you open it with --login it does source the profile. – xxcezz Jun 07 '14 at 16:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/55257/discussion-between-antonio-and-xxcezz). – antonio Jun 07 '14 at 16:51
  • @antonio - I have the same issue. I am using `mingw64_shell.bat` which starts mintty/bash with `start %WD%mintty -i /msys2.ico /usr/bin/bash --login %*`. I added some logging to `/etc/profile`, `/etc/bash.bashrc`, `~/.bash_profile`, `~/.profile`. with `bash --login`, only `/etc/profile` and `/etc/bash.bashrc` are executed/sourced. Whereas with cygwin, `~/.bash_profile` is sourced with `bash --login`. It seems this is a bug with `msys2`. See http://sourceforge.net/p/msys2/tickets/97/ – darcyparker Oct 24 '14 at 17:46
  • @antonio - I figured out solution... The `/etc/fstab` needs to be edited... see my answer below. – darcyparker Oct 24 '14 at 19:32
  • Having the same issue, but neither of the answers below work for me. – rsenna Nov 10 '15 at 11:28

2 Answers2

2

Disabling (renaming) system wide /etc/profile, ~/.profile is sourced.

After investigating /etc/profile I saw that, keeping it but commenting the function profile_d () ~/.profile is sourced. This function runs the scripts in /etc/profile.d/.

Disabling them individually I realized that the culprit is /etc/profile.d/bash_completion.sh.

It reads:

# Check for interactive bash and that we haven't already been sourced.
[ -z "$BASH_VERSION" -o -z "$PS1" -o -n "$BASH_COMPLETION_COMPAT_DIR" ] && return

# Check for recent enough version of bash.
bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
if [ $bmajor -gt 4 ] || [ $bmajor -eq 4 -a $bminor -ge 1 ]; then
    [ -r "${XDG_CONFIG_HOME:-$HOME/.config}/bash_completion" ] && \
        . "${XDG_CONFIG_HOME:-$HOME/.config}/bash_completion"
    if shopt -q progcomp && [ -r /usr/share/bash-completion/bash_completion ]; then
        # Source completion code.
        . /usr/share/bash-completion/bash_completion
    fi
fi
unset bash bmajor bminor

The first line explains why when running the subshell (the second time) things work: environment variables are already set, so the script returns.

The problem is that bash_completion.sh runs /usr/share/bash-completion/bash_completion, which is really huge and it is difficult to grasp the problem.

antonio
  • 9,285
  • 10
  • 59
  • 113
1

I was having the same issue.

I figured it out with the help of Dan in this ticket's comment thread: https://sourceforge.net/p/msys2/tickets/97

The solution is to edit the /etc/fstab

$ cat /etc/fstab
# For a description of the file format, see the Users Guide
# http://cygwin.com/cygwin-ug-net/using.html#mount-table

# DO NOT REMOVE NEXT LINE. It remove cygdrive prefix from path
none / cygdrive binary,posix=0,noacl,user 0 0

d:/Users/dparker /home/dparker ntfs binary,posix=0,user 0 0

Notice the last line is necessary to mount your home dir... I am not sure why you need to explicitly do this in /etc/fstab because it seems to mount it without it being there... but maybe it wasn't mounting it properly?

Hope this works for you like it did for me.

darcyparker
  • 1,191
  • 8
  • 12
  • This doesn't seem to work properly if the path has spaces in it, which many Windows user dirs do. Eg. `c:/Users/user\040name /home/user\040name ntfs ...`. I end up getting part of the "ntfs" in the mounted directory, ie `/home/user name nt` – Rebs Jun 22 '19 at 04:39