阅读bash中的命令帮助

阅读bash中的命令帮助,bash,Bash,我想读取数组中命令的输出: 比如: 使用输入框上方的{}重新格式化代码 var=`echo tell told till` echo "$var" | while read temp; do echo $temp ; done 此程序将输出: tell told till 我有两个问题: Read命令从stdin读取数据。但在上述情况下,它是如何阅读的?echo将值放在标准输出上。 我想知道如何将这些元素放入数组中?基本上,如何将元素放入数组的标准输出中。Read-命令可以将元素放入

我想读取数组中命令的输出:

比如:

使用输入框上方的{}重新格式化代码

var=`echo tell told till`
echo "$var" | while read temp; do echo $temp ; done  
此程序将输出:

tell 
told 
till
我有两个问题:

Read命令从stdin读取数据。但在上述情况下,它是如何阅读的?echo将值放在标准输出上。 我想知道如何将这些元素放入数组中?基本上,如何将元素放入数组的标准输出中。Read-命令可以将元素放入stdin中,但我需要stdout。
管道|将前面命令的标准输出连接到下面命令的标准输入。

管道|将前面命令的标准输出连接到下面命令的标准输入。

要将字符串拆分为单词,您只需使用:

for word in $string;
do 
    echo $word;
done;
那就照你说的做吧

while read line
do
    for word in $line
    do
        echo $word
    done
done

正如Ignacio Vazquez Abrams所说,一根管道将左侧的标准输出连接到右侧的标准输入。

要将字符串拆分为单词,您只需使用:

for word in $string;
do 
    echo $word;
done;
那就照你说的做吧

while read line
do
    for word in $line
    do
        echo $word
    done
done

正如Ignacio Vazquez Abrams所说,一条管道将左侧的标准输出连接到右侧的标准输出。

如果您想将标准输出中的元素放入数组,例如

declare -a array
array=( $(my command that generates stdout) ) #eg array=( $(ls))
如果您有一个变量并希望放入数组中

$> var="tell till told"
$> read -a array <<< $var
$> echo ${array[1]}
till
$> echo ${array[0]}
tell
从bash参考:

   Here Strings
       A variant of here documents, the format is:

              <<<word

       The word is expanded and supplied to the command on its standard input.

如果您想将stdout中的元素放入数组,例如

declare -a array
array=( $(my command that generates stdout) ) #eg array=( $(ls))
如果您有一个变量并希望放入数组中

$> var="tell till told"
$> read -a array <<< $var
$> echo ${array[1]}
till
$> echo ${array[0]}
tell
从bash参考:

   Here Strings
       A variant of here documents, the format is:

              <<<word

       The word is expanded and supplied to the command on its standard input.
使用管道时:

command1 | command2
写入标准输出的command1的输出将作为command2标准输入的输入。管道将标准输出转换为标准输入

对于阵列: 您可以为数组提供以下值:

array=(val1 val2 val3)
那就试试吧:

array=($var)
现在,在$array中有了$var:

> echo ${array[*]}
tell
told
till

> echo ${array[1]}
told
这就是你的意思吗?

使用管道时:

command1 | command2
写入标准输出的command1的输出将作为command2标准输入的输入。管道将标准输出转换为标准输入

对于阵列: 您可以为数组提供以下值:

array=(val1 val2 val3)
那就试试吧:

array=($var)
现在,在$array中有了$var:

> echo ${array[*]}
tell
told
till

> echo ${array[1]}
told

这就是你的意思吗?

那么为什么不回显$var | read-a temp,存储值tell,tell,till到temp数组中?那么为什么不回显$var | read-a temp,存储值tell,tell,till到temp数组中?这是一吨溶液。它起作用了!。你能告诉我一吨溶液的价格是多少吗。它起作用了!。你能告诉我是什么吗