小编Bri*_*sen的帖子

bash 脚本中的内置“read -r”在 Mac 上的行为有所不同

我有这个脚本:

#!/bin/bash
function main {
  while read -r file; do
    do_something "$file"
  done <<< $(find . -type f 2>/dev/null)
}
function do_something{
   echo file:$@
}
Run Code Online (Sandbox Code Playgroud)

在 Linux 上,它工作正常,但在 Mac(Bash 版本 5.2)上,它将找到的所有文件视为一项,并将整个字符串不带换行符传递到do_something. 如果我运行这个:

  while read -r file; do
    echo file:"$file"
  done <<< $(find . -type f 2>/dev/null)
Run Code Online (Sandbox Code Playgroud)

直接在 Mac 上的 Bash 终端中也可以正常工作。那么出了什么问题呢?

bash macintosh read

3
推荐指数
1
解决办法
371
查看次数

标签 统计

bash ×1

macintosh ×1

read ×1