【发布时间】:2015-11-12 20:28:43
【问题描述】:
我有一个包含一些命令输出的变量,我想计算该输出中的行数。我正在尝试使用纯 bash 来做到这一点(而不是像 this question 那样通过管道连接到 wc)。
我想出的最好的方法似乎有点麻烦:
function count_lines() {
num_lines=0
while IFS='' read -r line; do
((num_lines++))
done <<< "$1"
echo "$num_lines"
}
count_lines "$my_var"
有没有更简洁或更短的方法来做到这一点?
【问题讨论】:
-
为什么要避开
wc?但如果你绝对想避免它,你的功能似乎是要走的路。
标签: bash