我有一个使用 process substitution 的 shell 脚本

脚本是:

  #!/bin/bash
  while read line
  do
    echo "$line"
  done < <( grep "^abcd$" file.txt )

当我使用 sh file.sh 运行脚本时,我得到以下输出
$sh file.sh
file.sh: line 5: syntax error near unexpected token `<'
file.sh: line 5: `done < <( grep "^abcd$" file.txt )'

当我使用 bash file.sh 运行脚本时,脚本可以工作。

有趣的是, sh 是映射到 soft-link/bin/bash
$ which bash
/bin/bash
$ which sh
/usr/bin/sh
$ ls -l /usr/bin/sh
lrwxrwxrwx 1 root root 9 Jul 23  2012 /usr/bin/sh -> /bin/bash
$ ls -l /bin/bash
-rwxr-xr-x 1 root root 648016 Jul 12  2012 /bin/bash

我测试以确保使用以下命令在我的 shell 中跟踪符号链接(symbolic link):
$ ./a.out
hello world
$ ln -s a.out a.link
$ ./a.link
hello world
$ ls -l a.out
-rwx--x--x 1 xxxx xxxx 16614 Dec 27 19:53 a.out
$ ls -l a.link
lrwxrwxrwx 1 xxxx xxxx 5 May 14 14:12 a.link -> a.out

我无法理解为什么 sh file.sh 不作为 /bin/bash file.sh 执行,因为 sh/bin/bash 的符号链接(symbolic link)。

任何见解将不胜感激。谢谢。

最佳答案

当作为 sh 调用时,bash 进入 posix
读取启动文件后的模式。在 posix 模式下无法识别进程替换。根据 posix, <(foo) 应该直接从名为 (foo) 的文件输入。 (嗯,就是按照我的阅读标准,语法很多地方有歧义。)

编辑:从 bash manual :

The following list is what’s changed when ‘POSIX mode’ is in effect:
...
Process substitution is not available.

关于bash - 使用符号链接(symbolic link)时 sh 和 bash 之间的区别,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16551033/

10-11 22:22
查看更多