簡體   English   中英

將命令的輸出附加到Bash中的變量

[英]Appending output from a command to a variable in Bash

我試圖將命令的輸出附加到Bash中的變量。 我的代碼是

#!/bin/bash

for file in *
do
    lineInfo=`wc -l $file`
    echo "$lineInfo"
done

我了解如何使用反引號將命令的輸出“捕獲”到變量中,就像我在這一行中所做的那樣。

lineInfo=`wc -l $file`

有沒有一種干凈的方法可以將整個for循環的輸出放入Bash中的變量中? 還是在for循環的每次迭代中,將wc命令的輸出附加到linesInfo上? (不將任何內容重定向到文件)謝謝。

它將所有行信息(用逗號分隔)存儲到一個變量中並打印該變量:

#!/bin/bash

total=""

for file in *
do
    lineInfo=`wc -l $file`
    total="$total$lineInfo, "  # or total+="$lineInfo, "
done

echo $total

暫無
暫無

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

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