-2

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

They seem to achieve the same effect.

yosefrow
  • 2,128
  • 20
  • 29
  • The benefit would be not having to redirect everything with `bash -c`... – l'L'l Jun 04 '17 at 02:51
  • what do you mean by that? – yosefrow Jun 04 '17 at 02:59
  • `<<<` (here string), and redirection, that's what I mean. – l'L'l Jun 04 '17 at 03:00
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. Also see [Where do I post questions about Dev Ops?](http://meta.stackexchange.com/q/134306) – jww Jun 04 '17 at 14:23

1 Answers1

6
  • 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'
/
mklement0
  • 382,024
  • 64
  • 607
  • 775