When I open the WSL by clicking on its icon I need it to be opened in a certain directory (of course one that is more useful than System32), so I add a cd destination in my .bashrc. But then the "Open Linux Shell Here" option (available when you hold the Shift key on keyboard and right-click on any folder is also being redirected to that destination, which is undesirable. Is there a way I can detect this case in my .bashrc script and let it behave as it should?

In short, I need to know when the shell has been opened by the option in Shift+Right Click inside a folder, so that I can avoid changing the working directory to another one.

I'm using WSL-Ubuntu 18.04 LTS and GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu).


I would change the ~/.bashrc enhancement to something like this:

if [[ "$CurrDir" == "$SystemDir" ]]
    cd /path/to/sensible-dir-name

Where $SystemDir is your System32 directory. Always remember how \ and / differ between Windows and Linux paths:

Suggests using:

sed -e 's#^J:##' -e 's#\\#/#g'

That said in your instance you could probably hard-code the path.

  • You can use wslpath for the conversion: `if [[ "$CurrDir" == $(wslpath -u "$SystemDir") ]] – Carlos Rafael Ramirez Aug 2 '19 at 12:50
  • 1
    @CarlosRafaelRamirez $SystemDir is an empty variable for me – winklerrr Jan 18 at 8:28
  • @winklerrr SystemDir needs to be populated with your Windows System Directory name before you can test against it. – WinEunuuchs2Unix Jan 18 at 11:59

VSCode and TMUX compatible

You can add the following code to your ~/.bashrc file:

alias current_dir="pwd | sed -e 's/ /\\ /'"
alias save='echo $(current_dir) > ~/.saved_dir && echo "saved current dir, use \"load\" to cd to it"'
alias load='builtin cd $(cat ~/.saved_dir 2>/dev/null)'

if [ -n "$PS1" ] && [[ ! "$TERM" =~ screen ]] \
  && [[ ! "$TERM_PROGRAM" =~ vscode ]] \
  && [[ ! "$TERM" =~ tmux ]] && [ -z "$TMUX" ]; then
  # only load while in interactive console but not in vscode or not in tmux itself

  if [[ "${CURRENT_DIR,,}" == "/mnt/c/windows/system32" ]]; then
    # only load path if we start in default WSL system32 folder 
    # so "open in linux shell" still works

  if command -v tmux &> /dev/null; then 
    # only run tmux if it is installed
    exec tmux

The code checks if the terminal is run interactively and not inside a VSCode or TMUX session. If that's the case:

  • it loads a saved directory (see save/load) or your home directory if the start path is equal to the windows/system32 directory (${CURRENT_DIR,,} converts the string to lowercase for better matching)
  • it automatically starts TMUX if installed (just remove that if you don't like TMUX)

If you then want to change your start directory, just navigate to the directory you want to use as start directory and type save. Next time you start your terminal, that directory is loaded automatically.

Check out my dotfiles repo on GitHub for more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.