2つの使用法がありますbash
:
$ bash -c 'echo $0'
bash
$bash -c 'echo $0' "text"
text
パラメータが最初の場合はプログラム名を保存し、2番目の$0
場合は最初のパラメータを保存するのはなぜですか?
答え1
bash
マニュアルページによると:
-c If the -c option is present, then commands are read from the first non-option argument command_string. If there are argu‐ ments after the command_string, they are assigned to the positional parameters, starting with $0.
もっと下へ:
If arguments remain after option processing, and neither the -c nor the -s option has been supplied, the first argument is assumed to be the name of a file containing shell commands. If bash is invoked in this fashion, $0 is set to the name of the file, and the positional parame‐ ters are set to the remaining arguments.
つまり、$0
bashをどのように呼び出すかによって動作が異なります。