0

I've currently got an IP address in a variable and I'm trying to echo that variable with some string at the end:

echo $INTERFACE_IP
echo ":0.0"
echo "${INTERFACE_IP}:0.0"

And what I'm getting is:

192.168.240.238
:0.0
:0.0168.240.238

Expected output for the third line:

192.168.240.238:0.0

Full script:

#!/bin/bash
    
PS="/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe"

INTERFACE_INDEX=`${PS} Get-NetAdapter -InterfaceDescription "Cisco*" | awk '{print $8}'`
INTERFACE_IP=`${PS} Get-NetIPAddress -AddressFamily IPV4 -InterfaceIndex ${INTERFACE_INDEX} | grep IPAddress | cut -d ":" -f 2 | sed 's/ //g'`

echo $INTERFACE_IP                                                                                                                
echo ":0.0"

echo "$INTERFACE_IP:0.0"

export DISPLAY=$DISPLAY
doublespaces
  • 121
  • 3
  • 2
    I'm guessing the problem is the contents of INTERFACE_IP not being what you expect.... trailing CR? – Håkan Lindqvist May 28 '21 at 06:41
  • 1
    I'm pretty sure PowerShell is producing output in DOS/Windows text format, which includes a carriage return character (aka CR, `\r`, or `^M`) at the end of each line. – Gordon Davisson May 28 '21 at 16:01
  • @GordonDavisson Ahh, you know I was thinking something along those lines and dos2unix'd the file itself because I use WSL2 and sometimes create files in my Windows IDE. Please submit this as a solution and I'll check back later if it works. – doublespaces May 28 '21 at 17:19
  • @GordonDavisson you were right, this solved the problem: `sed -e 's/ //g' -e 's/\\r//g'` – doublespaces May 28 '21 at 17:32
  • @doublespaces I don't have a WSL2 system handy to check the syntax; why don't you write up the solution you found as a self-answer? – Gordon Davisson May 28 '21 at 18:32

0 Answers0