0

I'm using Windows 11 with WSL 2 Ubuntu 18.04 as Guest OS. I have written the following script as part of ~/.bash_profile with the intend to set the PRELUDE_HOST env variable both in my Ubuntu OS and on the Windows OS:

export PRELUDE_HOST=10.10.225.241
WINDOWS_PRELUDE_HOST=`cmd.exe /c echo %PRELUDE_HOST%`
if [[ "$PRELUDE_HOST" != "$WINDOWS_PRELUDE_HOST" ]]
then
    cmd.exe /c setx PRELUDE_HOST $PRELUDE_HOST
fi

The first time I run this script - the PRELUDE_HOST env is set onto the Windows OS globally. But for some reason, if I run the script a second time - the string comparison doesn't work as expected and the cmd.exe /c setx PRELUDE_HOST $PRELUDE_HOST runs a second time.

I can't figure out why the if [[ "$PRELUDE_HOST" != "$WINDOWS_PRELUDE_HOST" ]] comparison is not working as expected. Any suggestions?

mdzh
  • 964
  • 2
  • 14
  • 30
  • Please add to your question output of: `echo "$WINDOWS_PRELUDE_HOST" | hexdump -C` – Cyrus Mar 18 '22 at 11:01
  • Well apparently the strings are different. Try printing/logging them. – Gereon Mar 18 '22 at 11:02
  • 1
    Output of `cmd.exe` will have CRLF line ending. The CR will remain after its captured by `` `...` `` (should be `$(...)`). See [Are shell scripts sensitive to encoding and line endings?](https://stackoverflow.com/q/39527571/4154375) and [How to convert Windows end of line in Unix end of line (CR/LF to LF)](https://stackoverflow.com/q/3891076/4154375). – pjh Mar 18 '22 at 11:17
  • 1
    Try `"${WINDOWS_PRELUDE_HOST%$'\r'}"` to remove the carriage return. – dan Mar 18 '22 at 12:47
  • Removing the carriage return worked like a charm! Thank you! – mdzh Mar 19 '22 at 15:43

0 Answers0