簡體   English   中英

命令輸出和變量的行數不同,wc,bash

[英]Number of lines from command output and variable differs, wc, bash

wc直接命令輸出以及將輸出存儲在變量中時,為什么wc報告的行數不同?

$ virsh list --all | grep -P -- '(-|(\d+))\s+\S+\s+\S+'
$ var=$(virsh list --all | grep -P -- '(-|(\d+))\s+\S+\s+\S+')
$ wc -l <<< "${var}"
1
$ virsh list --all | grep -P -- '(-|(\d+))\s+\S+\s+\S+' | wc -l
0
$ echo "x${var}x"
xx
$ cat <<< ${var}

$

1.,4.調用建議命令( virsh | grep )不輸出任何內容。 5.調用建議該變量包含空字符串。 但是由於某種原因,wc在發票3中報告了一行。

問題出在這里字符串重定向(三尖括號)。 它可能會添加新行:

$ a=''
$ xxd <<< "$a"
00000000: 0a  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM