Conemu: Windows Subsystem For Linux Bash Specify Startup Directory

Created on 3 Aug 2016  ·  28Comments  ·  Source: Maximus5/ConEmu

ConEmu build: 160724 x64
OS version: Windows 10 x64
Used shell version (Far Manager, git-bash, cmd, powershell, cygwin, whatever): Windows Bash

Problem description

After following this guide I can't seem to find a way to specify a startup directory for bash. Linux paths (or the corresponding Windows path to the Linux directory) result in an error stating the directory name is invalid.

other-wsl

Most helpful comment

@NitroBAY Ops, I made a mistake.

The Windows file system is mounted to Windows Subsystem For Linux(WSL) on /mnt. For example, C:\Users\ is mapped to /mnt/C/Users. So on WSL, you can cd to /mnt/C/Users to access C:\Users\.

In order to make bash cd to particular directory, there are multiple ways.

  • On WSL side, you can add cd /home/NitroBAY/projects to the bottom of your .bashrc or .zshrc file in your home directory.
  • If you want to configure on ComEmu side, you can use "-new_console:d:C:\Users\llan\"%windir%\system32\bash.exe -cur_console:p for example. In this way, if you specify a path inside WSL, C:\Users\llan\AppData\Local\lxss\home\NitroBAY\projects for example, it always redirect to /.
  • The third method, you can configure on ComEmu like %windir%\system32\bash.exe -cur_console:p -c "cd /home/NitroBAY/projects && bash", I missed out bash at first. It might not be a good way, but it works.

All 28 comments

Not sure if this is the best solution, but I wrote a function to change the paths. You can put this in your .bashrc

function linuxpath()
{
echo $1 | sed 's_([A-Za-z]):_/mnt/\L1_g' | sed 's__/_g'
}

Then, on the linux side, you could call $(linuxpath C:\Windows\path\here\to\things) to get the corresponding /mnt/c/ style path.

Another option would be to switch to the directory you want first and the call bash.exe

@deto I think I wasn't clear enough with my question. I want to specify a startup directory in the VolFs. After reading up on it, I'm not sure it could be done, but I'll leave this open until someone can conclusively confirm or deny this.

@eygraber You can configure it like:

%windir%\system32\bash.exe -cur_console:p -c "cd ~"

-c means the command cd ~ is executed at bash startup. Of course you can set any command as you like.`

@clarkzjw I tried your command (on cmder which works like conemu) and it didn't work, so I don't know how to start in /home/NitroBAY/projects for instance and -new_console:d:C:\Users\Ilan\AppData\Local\lxss\home\NitroBAY\projects redirect to / for linux.... so I need to cd each time which lower somehow the interest of ConEmu ...

@NitroBAY Ops, I made a mistake.

The Windows file system is mounted to Windows Subsystem For Linux(WSL) on /mnt. For example, C:\Users\ is mapped to /mnt/C/Users. So on WSL, you can cd to /mnt/C/Users to access C:\Users\.

In order to make bash cd to particular directory, there are multiple ways.

  • On WSL side, you can add cd /home/NitroBAY/projects to the bottom of your .bashrc or .zshrc file in your home directory.
  • If you want to configure on ComEmu side, you can use "-new_console:d:C:\Users\llan\"%windir%\system32\bash.exe -cur_console:p for example. In this way, if you specify a path inside WSL, C:\Users\llan\AppData\Local\lxss\home\NitroBAY\projects for example, it always redirect to /.
  • The third method, you can configure on ComEmu like %windir%\system32\bash.exe -cur_console:p -c "cd /home/NitroBAY/projects && bash", I missed out bash at first. It might not be a good way, but it works.

@clarkzjw last solution works from ConEmu side but it's not very nice because before launch finishes some commands show up being executed and get clean up.

After following this guide I can't seem to find a way to specify a startup directory for bash.

This guide is just a disclaimer. The main idea is "just run bash.exe in ConEmu tab" and "report problems to Microsoft".

AFAIK you may pass startup directory as an argument to bash.exe, for example, "%windir%\system32\bash.exe" /mnt/c/Users. Anyway, that is the question for https://github.com/Microsoft/BashOnWindows, but not ConEmu.

Hey guys, I also found another solution - you can actually use Task parameters, too. The trick is not to include the/mnt/c part in your path, e.g.:

/dir "/Users" instead of /dir "/mnt/c/Users"

"/dir" is ConEmu's switch. It uses standard Windows paths, e.g. "c:\Users".

Works perfectly for me here - switches to the right directory on startup.

For other folks who come across this in attempts to Google a solution, here's the one that worked best for me. I changed the /etc/profile of the WSL to add these lines to the bottom:

if [ "$SHLVL" ] && [ "$SHLVL" = "1" ]; then
  cd ~
fi

For whatever reason, bash.exe is implicitly a login shell. There's no need to add --login to the command line. Per bash documentation, a login shell will source /etc/profile first. I found it helpful to use this as the earliest possible opportunity to fix the incorrect starting directory. It only does so when it's not a nested shell. That is, it's a shell that's started by ConEmu (or some other entity external from the WSL).

Note that you cannot otherwise specify to bash.exe that it should go to a different WSL directory. The "cd && bash -i" trick has subtle failings.

Putting @DerrickRice code into profile doesn't work for me, but if i put into .bashrc in home/user directory close and open bash work properly

As I am integrating WSL within VS Code I thought I would share what I found to work (for now anyways).

bash -c "cd /c/Users && bash --init-file ~/.bash_profile"

Granted this isn't accepting any arguments and may be fraught with Environment setup issues. It may be possible to create a Workspace .bash_profile for each project one is using that changes the cwd to the project's directory instead. Then the launching command can always be the same:

Use Case:

  • Create a .wsl directory in the root of your project folder.
  • Create a new .bash_profile file with the project settings required and changing the directory if needed.
  • Add a Terminal Launch string as follows: bash -c "cd . && bash --init-file ./.wsl/.bash_profile"

When the Terminal is launched it should source the local .bash_profile file. That file can then source a user common bash profile such as `~/.bash_profile' or such.

Perhaps this all is a non-issue now but I did come across something similar which is how I ended up here. I hope this can help anyone using ConEmu and WSL.

@afinnell What's the reason of your solution? In ConEmu startup directory for WSL works seamlessly without any -c cd ....
If you want to run wsl from your own application, you just need to specify startup directory for wsl.exe.

Emmm I think I've found a temporary solution to this issue....Here it is.
(I've replaced the WSL Ubuntu with Arch and edited .bashrc to switch to zsh automatically on startup, but it should work on the original one.)

First,

cp %CmderDir%/vendor/profile.ps1 %CmderDir%/vendor/profile_zsh.ps1
vim profile_zsh.ps1

add

function zsh () {
    bash -cur_console:p
}

after L37 (new alias -name "vi"....)
and

zsh

to the end of the file.

Then in the Cmder Settings-Startup-Tasks
Add new predefined tasks {zsh::WSL}
In the Commands window, add this:

PowerShell -ExecutionPolicy Bypass -NoLogo -NoProfile -NoExit -Command "Invoke-Expression '. ''%ConEmuDir%\..\profile_zsh.ps1'''" -new_console:d:"%USERPROFILE%"

and Select zsh::WSL as the startup task at Settings-Startup-Specified named task.

Then you can pass parameters to cmder.exe to specify its startup directory like running "cmder C:\" or creating a .ink file, and due to the right click-"Cmder Here" is just passing "%V" to cmder, when you "Cmder Here", WSL will start at current folder as you want it to. Wish @Maximus5 can add this new Startup task {bash::WSL} to the next version : )

But perhaps the best solution is that Microsoft can add a parameter for bash.exe like this:

bash --Initial-Command "<Command to run>"

@adorkablue What was the purpose of your post?
The {Bash::bash} is created in default ConEmu tasks (was implemented long ago).
The issue topic tells us "Specify Startup Directory" and you may do that seamlessly in the same way as with other tasks.
And I don't undestand how powershell function zsh is related to the issue.

P.S. cmder parameters are not related to ConEmu.

_(:з」∠)_ Great apologize for me messing Cmder and ConEmu... May I think that Cmder to ConEmu is like oh-my-zsh to zsh...?
I post this just because in Cmder the {bash::bash} just starts the MinGW32 bash, not the WSL one, and the "Right click-Cmder Here" doesn't pass any parameter like path to any bash either, so when I "Cmder Here", the bash actually starts at '\~‘ not 'here' ( ̄▽ ̄")...
Then I have a loot at the bash.exe Command Reference at https://msdn.microsoft.com/en-us/commandline/wsl/reference?f=255&MSPPError=-2147217396 but found no way to directly specify startup directory for WSL (@afinnell 's way may be a solution? But the command seems to be too long...), the only way to do this is to start a Powershell at the path, and then start bash (-cur_console:p to make arrow keys work) in the Powershell window, then path parameter in command "Cmder \ So in the profile_zsh.ps1, I add the zsh function and execute it when the powershell finishes init to enter WSL in the current path.
The windows terminal experience almost make me mad and maybe I should issue it to Cmder. Sorry for disturbing QAQ

I see absolutely no sense in cmder, especially if you use WSL. Cmder is just an additional layer of software, which may introduce unexpected effects.
Cmder.exe is a loader for ConEmu and the cmder bundle includes ConEmu, clink, mingw and other stuff.
What for if you already have is don't need them ?
You didn't even know that ConEmu itself supports WSL from the box for a long long time add haven't any problems with startup directories.

There should be some easy way to specify command to be run post lxss bash startup or specify startup directory in Linux Format (Eg. ~).

Moving to Home Directory post login is quite common but don't want to add it to .bash_profile

If you guys are using wslbridge in your task as specified on this page for proper ANSI colors, you can use -C per wslbridge help

Usage: /wsl/wslbridge [options] [--] [command]...
Runs a program within a Windows Subsystem for Linux (WSL) pty

Options:
  -C WSLDIR     Changes the working directory to WSLDIR first.
                An initial '~' indicates the WSL home directory.
  -e VAR        Copies VAR into the WSL environment.
  -e VAR=VAL    Sets VAR to VAL in the WSL environment.
  -T            Do not use a pty.
  -t            Use a pty (as long as stdin is a tty).
  -t -t         Force a pty (even if stdin is not a tty).

For me it looks something like

set "PATH=%ConEmuBaseDirShort%\wsl;%PATH%" & %ConEmuBaseDirShort%\conemu-cyg-64.exe --wsl -cur_console:pm:/mnt -C "~"

sudo nano ~/.bashrc

move cursor to the bottom of the file,
press enter [make a new line]

cd /mnt/c/User/<username>/<desired>/<path>

@13ck015405 Bad advice. All consoles would be opened in your path hardcoded in .bashrc. And there would be no way to avoid it.

As @leonyu mentioned, I wish there is an easy way to do this. For now, I gave up and added cd ~ in .bashrc

There is easy way to do that. Documented.
No need to harm your .bashrc

@Maximus5 oh thanks! just tried and works perfectly. Btw, thanks for building and maintaining conemu, it's a great utility and worked very well for me so far.

Since this is now the top search result for "conemu wsl startup directory" and the comments are a little noisy; the solution is to add -C "~" to the end of your commands string. If you select the {WSL:bash} task in Startup > Tasks the command text box should look like this:

set "PATH=%ConEmuBaseDirShort%\wsl;%PATH%" & %ConEmuBaseDirShort%\conemu-cyg-64.exe --wsl -cur_console:pm:/mnt -C "~"

I added exact links to the table of contents
https://conemu.github.io/en/TableOfContents.html#bash-on-windows

@NitroBAY Ops, I made a mistake.

The Windows file system is mounted to Windows Subsystem For Linux(WSL) on /mnt. For example, C:\Users\ is mapped to /mnt/C/Users. So on WSL, you can cd to /mnt/C/Users to access C:\Users\.

In order to make bash cd to particular directory, there are multiple ways.

  • On WSL side, you can add cd /home/NitroBAY/projects to the bottom of your .bashrc or .zshrc file in your home directory.
  • If you want to configure on ComEmu side, you can use "-new_console:d:C:\Users\llan\"%windir%\system32\bash.exe -cur_console:p for example. In this way, if you specify a path inside WSL, C:\Users\llan\AppData\Local\lxss\home\NitroBAY\projects for example, it always redirect to /.
  • The third method, you can configure on ComEmu like %windir%\system32\bash.exe -cur_console:p -c "cd /home/NitroBAY/projects && bash", I missed out bash at first. It might not be a good way, but it works.

In my case, I would like ConEmu to start Bash up always in this directory:
/root/myprojectname

For your second and third solution, could you explain where I would put those codes? Is it in ConEmu settings - Startup - Tasks, then choosing {Bash::bash}? Right now I have this line of code:
set "PATH=%ConEmuBaseDirShort%\wsl;%PATH%" & %ConEmuBaseDirShort%\conemu-cyg-64.exe --wsl -cur_console:pm:/mnt
How should update this?

Was this page helpful?
0 / 5 - 0 ratings