Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.bashrc run twice for non-interactive shell with BASH_ENV shared via WSLENV #2897

Closed
diablodale opened this issue Jan 31, 2018 · 2 comments
Closed

Comments

@diablodale
Copy link

.bashrc is running twice when a non-interactive bash shell is started with bash -c while at the same time BASH_ENV is set to ~/.bashrc and shared with WSLENV=BASH_ENV

Setup

Microsoft Windows [Version 10.0.17074.1002]
Clevo P150em laptop with Intel i7-3720qm and 16GB ram
Current Windows Updates
Current apt updates/upgrade

Repo

  1. In WSL, edit the default .profile and at the top add echo "running profile"
  2. In WSL, edit the default .bashrc and at the top add echo "running bashrc"
  3. Also in .bashrc, locate the case/esac block which begins with # If not running interactively, don't do anything
  4. Under the esac of that block, add echo "interactive"
  5. Close all WSL windows, WSL background tasks, etc.
  6. Open a Windows command prompt
  7. Type bash and press enter.
  8. Verify you got three lines running profile, running bashrc, and interactive.
    If you did not get three lines, go back to step 1 above and troubleshoot.
  9. Type exit and press enter.
  10. Type bash -c "echo hello" and press enter.
  11. Verify you got a single line hello.
    If you did not get that single line, stop and troubleshoot.
  12. Type set BASH_ENV=~/.bashrc and press enter
  13. Type set WSLENV=BASH_ENV and press enter
  14. Type bash -c "echo hello" and press enter.

Result

Three lines: running bashrc, running bashrc, hello

Expected

Two lines: running bashrc, hello

@diablodale
Copy link
Author

This could be related to post at https://superuser.com/questions/671372/running-command-in-new-bash-shell-with-rcfile-and-c

Is
...windows bash.exe the first shell (which runs what is defined at BASH_ENV)
...and then a second bash shell (which runs what is defined at BASH_ENV) is run by something to execute the -c "echo hello"
...resulting in two runs of .bashrc ??

If yes, then this is expected wonky behavior and putting a unset BASH_ENV at the top of .bashrc is the fix and there is no issue with WSL.
If no, then something more needs investigation.

@therealkenc
Copy link
Collaborator

@diablodale. Yes you are looking at the same case as the superuser post, for sure. This is all grounded in the semantics of (the tragically named) bash.exe. If I do:

$ ssh localhost "BASH_ENV=.bashrc echo hello"

That returns two lines not three. What bash.exe is doing is the equivalent of:

$ ssh localhost "BASH_ENV=.bashrc bash -c 'echo hello'"

Which gives three lines.

This was improved with the introduction of wsl.exe per #846 (message). The semantics you probably want are like the first ssh analogy:

C:\> set BASH_ENV=~/.bashrc
C:\> set WSLENV=BASH_ENV
C:\> wsl.exe echo hello

Which returns two lines. Ben will have to weigh in for closure.

diablodale added a commit to diablodale/wslgit that referenced this issue Feb 2, 2018
diablodale added a commit to diablodale/wslgit that referenced this issue Feb 5, 2018
diablodale added a commit to diablodale/wslgit that referenced this issue Mar 2, 2018
* no-i:
  conditional .bashrc loading for Win10 builds 17063+ - Win10 builds 17063+ support sharing env vars w/ WSL - when WSLENV and BASH_ENV are shared, will launch   WSL as non-interactive - Win builds prior (e.g. FCU) will not get this feature
  Use wsl.exe to workaround double run of .bashrc - workaround for microsoft/WSL#2897
  call WSL git non-interactively yet still with .bashrc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants