使用bash -c比使用here字符串有什么好处

yos*_*row -2 linux bash herestring

使用bash -c 'some command'过度使用是否有任何实际好处bash <<< 'some command'

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

mkl*_*nt0 6

  • bash -c '...' 你可以选择为命令提供stdin输入,

  • bash <<<'...'排除了该选项,因为stdin已被用于提供脚本执行.

例子:

# 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'
/
Run Code Online (Sandbox Code Playgroud)