Skip to content
Advertisement

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

Is there any real benefit to using bash -c 'some command' over using bash <<< 'some command'

They seem to achieve the same effect.

Advertisement

Answer

  • bash -c '...' leaves you the option to provide stdin input to the command,

  • whereas bash <<<'...' precludes that option, because stdin is already being used to provide the script to execute.

Examples:

# 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'
/
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement