2

I'm trying to create the following alias in Ubuntu WSL:

alias windocs='cd /mnt/c/Users/Firstname L'astname/Documents/'

My username is unfortunately Firstname L'astname, including the space and apostrophe.

I've tried using backslashes:

alias windocs='cd /mnt/c/Users/Firstname\ L\'astname/Documents/'

But bash still interprets the apostrophe as the end of the string, resulting in the following error:

-bash: .bash_aliases: line 11: unexpected EOF while looking for matching `''
-bash: .bash_aliases: line 12: syntax error: unexpected end of file

I've also tried encapsulating the username in double-quotation marks:

alias windocs='cd /mnt/c/Users/"Firstname L'astname"/Documents/'

And I get the same error.

How can I write this alias so that it works? It's a pain having to cd all the way to the Documents folder every time.

3 Answers 3

3

Single quotes in a single-quoted string aren't escapable, but luckily, you can just use double quotes around the alias definition and then (escaped) double quotes around the directory name:

alias windocs="cd \"/mnt/c/Users/Firstname L'astname/Documents/\""

Alternatively, you could symlink that directory into your Linux home directory:

ln -s "/mnt/c/Users/Firstname L'astname/Documents/" ~/docs

And then, simply cd docs (or cd ~/docs, if you're not in your home dir) will take you there.

3
  • I didn't even think of using ln -s, smart! Aug 21, 2019 at 20:17
  • 1
    @JoshuaO'Reilly In situations where it's impractical to use double-quotes (e.g. because there's also lots of double-quotes or $ signs or other characters that would require a lot of ugly escaping), you can end the single-quote ('), escape a single quote (\'), and then begin the single-quoting again ('). i.e. '\''. alias windocs='cd /mnt/c/Users/Firstname L'\''astname/Documents/'. As long as there is no unescaped white-space outside of any quotes, the shell will still treat it as one argument.
    – cas
    Aug 22, 2019 at 2:38
  • Wow, there's a lot of ways to go about doing this. Thanks! Aug 22, 2019 at 13:58
1

Well, I should have looked this up first. I put the entire string in double-quotation marks as per this stackoverflow answer:

alias windocs="cd /mnt/c/Users/Firstname\ L\'astname/Documents/"

and it works now. This is because there is no escape mechanism from single-quote strings, but there is for double quote strings (as per the other answer).

0
1

For Windows Subsystem for Linux (WSL) only, Windows side environment variables can be mapped into $WSLENV environment variable in WSL side. For example, the /mnt/c/Users/username/ in Windows side is saved as %USERPROFILE% environment variable.

To do this, first add the variable in Windows by running this command as administrator in Command Prompt

setx WSLENV USERPROFILE

Then add the alias in .bashrc (or similar files). For example,

alias windocs='cd $(wslpath $USERPROFILE/Documents)'

Here wslpath translates Windows style path (backward slash) to *NIX style path (forward slash). In schematics,

   Windows        <--->      WSL
C:\Users\username <--->  /mnt/c/Users/username
USERPROFILE       <--->  WSLENV

Read Share Environment Vars between WSL and Windows blog for further information.

3
  • May I ask why this is down-voted?
    – Biswapriyo
    Aug 21, 2019 at 17:11
  • I didn't downvote, but I don't see where you're answering the question. This is useful information, but how would you use it to make the alias that the OP is asking for?
    – terdon
    Aug 21, 2019 at 17:14
  • terdon's solution is more in line with my current approach, but it's good to know there are other ways to do this. Thanks! Aug 21, 2019 at 20:21

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.