0

I use WSL2, and need an environment variable called DISPLAY to communicate with XServer which is running on my host (windows) IP. I am trying to set this environment each time my shell starts using my .bashrc. It has to update each time because my IP is dynamically assigned.

Here is the section of the code in the .bashrc responsible for setting the DISPLAY environment variable.

alias windowsip="ipconfig.exe | grep IPv4 | cut -d: -f2 | head -1 | sed 's/^[ \t]*//;s/[ \t]*$//'"
WINDOWSIP="$(windowsip)"
ENDING=":0.0"

export DISPLAY="$WINDOWSIP$ENDING"

Although the .bashrc runs quietly, there are a number of things that indicate that the environment variable was not set as desired.

Firstly,

bensmus@BENSMUSPC:~$ $DISPLAY
:0.0: command not found
bensmus@BENSMUSPC:~$

This is strange because my windowsip command works fine, and gives output without any leading spaces.

bensmus@BENSMUSPC:~$ windowsip
123.123.1.23
bensmus@BENSMUSPC:~$

(Note: My ip is not 123.123.1.23, I just feel weird about sharing my IP)

Even more strangely, when I do

bensmus@BENSMUSPC:~$ echo $DISPLAY
:0.0123.1.23

What the heck is going on?

I really want it to just be

bensmus@BENSMUSPC:~$ echo $DISPLAY
123.123.1.23:0.0
xdhmoore
  • 8,935
  • 11
  • 47
  • 90
Benjamin Smus
  • 103
  • 1
  • 7
  • 2
    Unrelated: Don't give the command `$DISPLAY`. Do `echo $DISPLAY` to see what its value is. About the IP: Does it start with 192.168.? :) – Ted Lyngmo Jan 16 '21 at 19:49
  • 2
    `What the heck is going on?` Your file has dos line endings. Also, what's the point? There is localhost - 127.0.0.1 - to connect with... local host. And `:0.0` without any ip will connect to localhost... so why do you set it at all? – KamilCuk Jan 16 '21 at 19:49
  • I don't understand how localhost is applicable here. I need to connect to the IP of my Windows machine that is running WSL 2. – Benjamin Smus Jan 16 '21 at 19:54
  • @TedLyngmo yes it does! – Benjamin Smus Jan 16 '21 at 19:56
  • 2
    @BenjaminSmus The 192.168.x.x address is your LAN IP address. Your WAN IP address (that can be used to try to connect to your network) is something completely different. My LAN IP is 192.168.1.140 for example. – Ted Lyngmo Jan 16 '21 at 20:01
  • @KamilCuk Just based on experimentation, :0.0 is necessary in my case. Currently what I do to use XServer is run `export DISPLAY=192.168.1.23:0.0`, and if I don't include :0.0, it doesn't work – Benjamin Smus Jan 16 '21 at 20:02
  • Btw, what if you do: WINDOWSIP="$(ipconfig.exe | grep IPv4 | cut -d: -f2 | head -1 | sed 's/^[ \t]*//;s/[ \t]*$//') instead? Same problem? Hmm, is `ipconfig.exe` even available? I don't have that in my WSL2 bash shell. – Ted Lyngmo Jan 16 '21 at 20:02
  • Exactly the same thing @TedLyngmo, yes ipconfig.exe works! – Benjamin Smus Jan 16 '21 at 20:05
  • To see exactly what's in that variable: `printf '%s' "$DISPLAY" | od -c` or `printf '%q\n' "$DISPLAY"` – glenn jackman Jan 16 '21 at 20:14
  • A little late but I realized you can just pipe the output from `ipconfig.exe` thru `dos2unix`. I've updated my answer. – xdhmoore Jan 16 '21 at 21:18

1 Answers1

2

It looks like there is a char 0d character at the end of the data returned by the windowsip alias (probably because of the output encoding of ipconfig.exe):

myprompt~$ echo $DISPLAY
:0.0168.1.7

myprompt~$ echo $DISPLAY | xxd
00000000: 3139 322e 3136 382e 312e 370d 3a30 2e30  192.168.1.7.:0.0
00000010: 0a

Since 0d is the carriage return, I guess the weird result of printing $DISPLAY is caused by the print logic returning to the beginning of the line when it encounters 0d. But maybe that was obvious to everyone besides me...

Better Answer:

Since this is just a windows/unix newline issue, running ipconfig.exe through dos2unix seems to fix the problem:

alias windowsip="ipconfig.exe | dos2unix | grep IPv4 | cut -d: -f2 | head -1 | sed 's/^[ \t]*//;s/[ \t]*$//'"

Leading to:

myprompt:~$ echo $DISPLAY
192.168.1.7:0.0
myprompt~$ echo $DISPLAY | xxd
00000000: 3139 322e 3136 382e 312e 373a 302e 300a  192.168.1.7:0.0.

Previous Answer:

I'm not sure why there is an 0d in the middle and then a 0d at the end, but if you remove both with sed, it seems to work for me:

alias windowsip="ipconfig.exe | grep IPv4 | cut -d: -f2 | head -1 | sed 's/^[ \t]*//;s/[ \t]*$//;s/[\n\r]//g;'"

This leads to:

myprompt~$ echo $DISPLAY
192.168.1.7:0.0
myprompt~$ echo $DISPLAY | xxd
00000000: 3139 322e 3136 382e 312e 373a 302e 300a  192.168.1.7:0.0.

I'm not sure why it has a 0a at the end, but that seems to be the case if you just set it to a string:

myprompt~$ DISPLAY="192.168.1.7:0.0"
myprompt~$ echo $DISPLAY | xxd
00000000: 3139 322e 3136 382e 312e 373a 302e 300a  192.168.1.7:0.0.
xdhmoore
  • 8,935
  • 11
  • 47
  • 90
  • 1
    It's because the .bashrc file itself contains CRNL line endings, so the variable assignment is actually seen as `WINDOWSIP="$(windowsip)"\r` and `ENDING=":0.0"\r` -- sed is not the answer, removing the carriage returns from the dotfile is. – glenn jackman Jan 16 '21 at 20:24
  • An alternative using `awk`: `ipconfig.exe | grep IPv4 | head -1 | dos2unix | awk '{ print $NF }'` – Ted Lyngmo Jan 17 '21 at 04:54