使用 bash -c 比使用此处字符串有什么优势

What is the advantage of using bash -c over using a here string

使用 bash -c 'some command' 比使用 bash <<< 'some command'

有什么真正的好处吗

他们似乎达到了同样的效果。

  • bash -c '...' 让您可以选择为命令提供标准输入,

  • bash <<<'...' 排除了该选项,因为标准输入已被用于提供要执行的脚本。

示例:

# Executes the `ls` command then processes stdin input via `cat`
echo hi | bash -c 'ls -d /; cat -n'
/
     1  hi

# The here-string input takes precedence and pipeline input is ignored.
# The `ls` command executes as expected, but `cat` has nothing to read, 
# since all stdin input (from the here-string) has already been consumed.
echo hi | bash <<<'ls -d /; cat -n'
/