什么是/ bin/sh -c?

use*_*831 23 linux bash

什么/bin/sh -c意思?怎么-c办?

aio*_*obe 24

从bash的手册页:

-c string If  the  -c  option  is  present, then commands are read from
          string.  If there are arguments after the  string,  they  are
          assigned to the positional parameters, starting with $0.
Run Code Online (Sandbox Code Playgroud)

例:

$ bash -c ls
Run Code Online (Sandbox Code Playgroud)

将启动bash并执行命令ls.

/bin/sh 通常是shell的符号链接.

  • 您可以将脚本作为参数传递,`bash myScript` 但它会打开 `myScript` 并解释命令。这与`bash -c someCommand` 不同。例如,比较如果你执行 `bash ls` 和 `bash -c ls` 会发生什么。 (6认同)
  • 没有`-c`会有什么不同? (3认同)
  • 我知道了。它需要格式化字符串。/bin/sh -c 'grep "$0" /proc/cpuinfo | 唯一| sed -e "$1"' "cpu cores" 's/[^0-9]*//g' 就像一个魅力。 (2认同)