0

I assigned the windows' user name to a variable in WSL Ubuntu Bash with this command:

WINUSER=`cmd.exe /C 'echo %username%'`

If I echo the variable, it behaves normally:

$ echo $WINUSER
myusername

However, if I try to insert it into a string, every character I place AFTER the variable will replace the characters at the beginning of the string, like this:

echo "/mnt/c/User/$WINUSER/Documents"
/Documentsr/myusername

While, if I don't place characters after the strings:

echo "/mnt/c/User/$WINUSER"
/mnt/c/User/myusername

It prints correctly to stout, but I still can't cd /mnt/c/User/$WINUSER

Since it doesn't occur with other variables, such as $HOME, I guess it depends by the output of the windows-wsl inter-operability. How could I solve this?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
bepoli
  • 31
  • 8
  • I guess you have a carriage return at the end of the string returned from `cmd.exe`. What do you see for `printf '%q\n' "$WINUSER"`? – Benjamin W. Aug 03 '18 at 14:26
  • @BenjaminW. I get `$'myusername\r'` in the shell output – bepoli Aug 03 '18 at 14:30
  • It's a carriage return. You can get rid of it with something like `"${WINUSER//$'\r'}"` when you use the variable. – Benjamin W. Aug 03 '18 at 14:35
  • That fixed it, thanks! Also, thanks for posting a reference. That could definitely apply as duplicate to this question, feel free to cast a close vote. – bepoli Aug 03 '18 at 15:06
  • I used the wrong duplicate at first, now I can't vote any longer :/ – Benjamin W. Aug 03 '18 at 15:07

0 Answers0