且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Bash遍历每行并使用每行的第一个单词和第二个单词运行命令

更新时间:2023-02-06 19:15:19

BashFAQ#1 ,而边读循环则是适合该作业的工具:

Per BashFAQ #1, a while read loop is the appropriate tool for the job:

while read -r first_word second_word rest; do
  your_command "$first_word" "$second_word"
done <<<"$var"

请注意 rest 的使用-具有占位符( _ 也很常见)意味着第二个之后的单词也不会放置在 second_word中变量.

Note the use of rest -- having a placeholder (_ is also common) means that words after the second one are not also placed in the second_word variable.