bash에서 `function Name { ...; }`, `name() { ... }` 및 `function name() { ...;

bash에서 `function Name { ...; }`, `name() { ... }` 및 `function name() { ...;

여러 가지 방법으로 bash 함수를 작성할 수 있습니다.

function JoinStrings {
    ...;
}

또는

function JoinStrings () {
    ...;
}

또는

JoinStrings () {
    ...;
}

이 기능들 사이에 차이점이 있나요? Bash에서 함수를 작성하는 방법이 3가지인 이유는 무엇입니까? (함수를 작성하는 방법이 더 있나요?)

답변1

man bash설명하다:

Shell Function Definitions
   A  shell function is an object that is called like a simple command and exe‐
   cutes a compound command with a new set  of  positional  parameters.   Shell
   functions are declared as follows:

   name () compound-command [redirection]
   function name [()] compound-command [redirection]
          This  defines  a  function named name.  The reserved word function is
          optional.  If the function reserved word is supplied, the parentheses
          are  optional.  The body of the function is the compound command com‐
          pound-command (see Compound Commands above).  That command is usually
          a  list  of  commands  between { and }, but may be any command listed
          under Compound Commands above.  compound-command is executed whenever
          name  is  specified  as  the name of a simple command.  When in posix
          mode, name may not be the name of one of the POSIX special  builtins.
          Any redirections (see REDIRECTION below) specified when a function is
          defined are performed when the function is executed.  The exit status
          of  a  function  definition is zero unless a syntax error occurs or a
          readonly function with the same name already exists.  When  executed,
          the  exit status of a function is the exit status of the last command
          executed in the body.  (See FUNCTIONS below.)

간단히 말해서 차이가 없습니다.

관련 정보