Setting argv0
Could there be command line flag added for setting argv0?
Here's the problem: when shells start, they check argv0 for a leading dash, to know if they are a login shell, in which case they'll source user profile files (~/.profile, for example).
The unix login program will typically start a user shell (let's use bash as an example) like this:
- /bin/bash <-- Program to exec
- -bash <-- argv[0]
Cygwin's mintty has this feature, for example. The result without providing argv0 is mintty/wsltty#37.
I looked at a bunch of shells, and they all had -l (or --login) options for configuring the shell as a login shell. My wsltty install is a bit old, but I think the default Explorer shortcut passes -l. I just tried it now, and it sourced my ~/.bash_profile file correctly. Of course, the file has to be in the WSL home directory (e.g. C:\Users\IEUser\AppData\Local\lxss\home\IEUser), not the Cygwin/MSYS2 home directory.
By default, I think I was trying to have wslbridge match what the "Bash on Ubuntu" shortcut does, which invokes C:\Windows\System32\bash.exe ~. I don't think the ~ is a normal Unix argument? I noticed that my default WSL homedir has a .bashrc file that sets up a bunch of stuff, and the default "Bash on Ubuntu" shortcut sources the .bashrc file. If I run bash.exe --login, though, it stops sourcing .bashrc. I think there's normally a profile/login script that's sourced instead, and that script then includes .bashrc? There's no bash login/profile file in my WSL homedir, though.
I didn't see any option (e.g. with mintty or ssh) that lets me configure an arbitrary argv0 with a dash. I did notice that mintty can be invoked with just a -, and then it runs bash (with -bash for argv0). I see that ssh defaults to a -bash argv0, as does a normal Unix login (e.g. using Ctrl-Alt-F1 on my Ubuntu machine).
Comments / questions:
- Can you get what you need with the
-l/--loginshell option? - Maybe
wslbridgeshould default to matching ssh behavior and pass-bashfor argv0. - Maybe
wslbridgecan instead matchminttyand interpret a-command-line as "invoke using-bash". - Is it normal for the Ubuntu login shell not to include
~/.bashrc? What about the latest WSL/Ubuntu versions? - What if the user configures a non-bash WSL shell?
I checked a newer WSL installation, and now I see a ~/.profile script that sources ~/.bashrc.
What if the user configures a non-bash WSL shell?
It probably involves getpwuid(getuid())->pw_shell. I see that mintty (and Konsole?) prefer the SHELL environment variable, but I'm guessing that will always be bash, given that the WSL environment is always initialized by the bash.exe wrapper.
I tried the trivial thing of adding -l after /bin/bash to my shortcut, argv0 still /bin/bash rather than -bash. I'll play with this more.
Maybe I did it wrong? How do I pass additional arguments to the program started by wslbridge?
Here's my shortcut:
%LOCALAPPDATA%\wsltty\bin\mintty.exe --wsl -o Locale=C -o Charset=UTF-8 /bin/wslbridge -C~ -t /bin/bash -l
I see I also need to research how mintty handles arguments!
argv0 would still be /bin/bash, but bash would run the login/profile script instead of bashrc. From the bash man page:
INVOCATION
A login shell is one whose first character of argument zero is a -, or
one started with the --login option.
...
When bash is invoked as an interactive login shell, or as a non-inter‐
active shell with the --login option, it first reads and executes com‐
mands from the file /etc/profile, if that file exists. After reading
that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile,
in that order, and reads and executes commands from the first one that
exists and is readable.
...
When an interactive shell that is not a login shell is started, bash
reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if
these files exist.
I just installed wsltty 0.7.6, and it looks like it created three shortcuts -- one labeled %, one labeled ~, and one labeled -l. If I open wsltty with the first two, then close it using Ctrl-D, then I can see exit briefly flash before the window disappears. If I use the last one, then I'll see logout flash instead.
It occured to me that mintty's behavior makes more sense for mintty, because mintty is a Cygwin program that inherits the Cygwin environment. wslbridge, on the other hand, is more like ssh in that its Cygwin environment is discarded in favor of a new WSL environment.
See also my response https://github.com/mintty/wsltty/issues/37#issuecomment-294439518. The first two shortcuts mentioned by rprichard are intended to start in dedicated directories. Assuming the WSL .profile will typically navigate to the WSL home directory, they cannot be login shells. The third is a login shell.
I'll test the 3rd shortcut with -l in the name. When I installed, I got 2 shortcuts, not 3, so let me check if I have an older version. Thanks!
Note that the standard Bash on Ubuntu shortcut does not start a login shell either. The WSL invocation wrapper C:\Windows\System32\bash.exe can be started with an -l option, with the effect that your login .profile is activated. But $0 will still be /bin/bash then. Combining both observations, behaviour of the wsltty "~" shortcut is consistent.
We might try whether, if the wrapper is activated with a $0 of "-bash", it propagates that to WSL.