简体   繁体   English

我如何将 append 转换为来自 powershell 的 bash 变量?

[英]How do I append to a bash variable that is from a powershell output?

I am using WSL and need to get the computers IP address and append ":0.0".我正在使用 WSL,需要获取计算机 IP 地址和 append“:0.0”。 I execute a powershell command to get the IP address (random IP below) and assign that to a variable.我执行 powershell 命令来获取 IP 地址(下面是随机的 IP)并将其分配给一个变量。 Appending to that variable overwrites it.附加到该变量会覆盖它。

> IP=$(powershell.exe "(Test-Connection -ComputerName (hostname) -Count 1).IPV4Address.IPAddressToString")
> echo $IP
127.27.15.1
> echo "${IP}:0.0"
:0.027.15.1

How do I get it to add it to the end instead of replacing the first 4 characters?如何让它添加到末尾而不是替换前 4 个字符?

Remove the Windows line end from the value:从值中删除 Windows 线端:

echo "${IP%$'\r'}:0.0"

Command substitution $(...) removes the final \n , but doesn't remove the \r that precedes it.命令替换$(...)删除最后的\n ,但不删除它之前的\r

Parameter expansion ${var%pattern} removes the pattern from the end of the var's value.参数扩展${var%pattern}从 var 值的末尾删除模式。 $'...' enables the ANSI C escapes. $'...'启用 ANSI C 转义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM