bash의 첫 번째 인수는 인수가 전달되거나 전달되지 않을 때 다릅니다.

bash의 첫 번째 인수는 인수가 전달되거나 전달되지 않을 때 다릅니다.

두 가지 사용법이 있습니다 bash:

$ bash -c 'echo $0'
bash

$bash -c 'echo $0' "text"
text

매개변수가 첫 번째 경우에는 프로그램 이름을 저장 $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.

즉, $0bash를 어떻게 호출하느냐에 따라 동작이 달라집니다.

관련 정보