Positional Parameters

範例 ex_001

ex_001.sh

#!/usr/bin/env bash

echo '$0:' $0
echo '$1:' $1
echo '$2:' $2
echo '$3:' $3
echo '$4:' $4
echo '$5:' $5
echo '$6:' $6
echo '$7:' $7
echo '$8:' $8
echo '$9:' $9
echo '${10}:' ${10}
echo '${11}:' ${11}
echo '${12}:' ${12}
echo '${13}:' ${13}
echo '${14}:' ${14}
echo '${15}:' ${15}

執行

$ ./ex_001.sh a b c d e f g h i j k l m n o

顯示

$0: ./ex_001.sh
$1: a
$2: b
$3: c
$4: d
$5: e
$6: f
$7: g
$8: h
$9: i
${10}: j
${11}: k
${12}: l
${13}: m
${14}: n
${15}: o

範例 ex_002

ex_002.sh

#!/usr/bin/env bash

test_func () {

    echo '$FUNCNAME:' $FUNCNAME
    echo '$0:' $0
    echo '$1:' $1
    echo '$2:' $2
    echo '$3:' $3
    echo '$4:' $4
    echo '$5:' $5
    echo '$6:' $6
    echo '$7:' $7
    echo '$8:' $8
    echo '$9:' $9
    echo '${10}:' ${10}
    echo '${11}:' ${11}
    echo '${12}:' ${12}
    echo '${13}:' ${13}
    echo '${14}:' ${14}
    echo '${15}:' ${15}

}

test_func a b c d e f g h i j k l m n o

執行

$ ./ex_002.sh

顯示

$FUNCNAME: test_func
$0: ./ex_002.sh
$1: a
$2: b
$3: c
$4: d
$5: e
$6: f
$7: g
$8: h
$9: i
${10}: j
${11}: k
${12}: l
${13}: m
${14}: n
${15}: o

文件說明

執行

$ man bash

可以看到下面一段說明

Positional Parameters
    A positional parameter is a parameter denoted by one or more digits, other than the single digit 0.  Positional
    parameters  are  assigned  from  the  shell's arguments when it is invoked, and may be reassigned using the set
    builtin command.  Positional parameters may not be assigned to  with  assignment  statements.   The  positional
    parameters are temporarily replaced when a shell function is executed (see FUNCTIONS below).

    When  a  positional parameter consisting of more than a single digit is expanded, it must be enclosed in braces
    (see EXPANSION below).

可以看到下面一段說明

Special Parameters
    The shell treats several parameters specially.  These parameters may only be referenced; assignment to them  is
    not allowed.
...略...
    0      Expands  to  the  name  of  the shell or shell script.  This is set at shell initialization.  If bash is
           invoked with a file of commands, $0 is set to the name of that file.  If bash is  started  with  the  -c
           option, then $0 is set to the first argument after the string to be executed, if one is present.  Other‐
           wise, it is set to the filename used to invoke bash, as given by argument zero.

可以看到下面一段說明

ARGUMENTS
       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 parameters are set to the remaining arguments.   Bash  reads
       and  executes  commands  from this file, then exits.  Bash's exit status is the exit status of the last command
       executed in the script.  If no commands are executed, the exit status is 0.  An attempt is first made  to  open
       the  file  in  the current directory, and, if no file is found, then the shell searches the directories in PATH
       for the script.

可以看到下面一段說明

FUNCTIONS
       A shell function, defined as described above under SHELL GRAMMAR, stores a series of commands for later  execu‐
       tion.  When the name of a shell function is used as a simple command name, the list of commands associated with
       that function name is executed.  Functions are executed in the context of the current shell; no new process  is
       created  to  interpret them (contrast this with the execution of a shell script).  When a function is executed,
       the arguments to the function become the positional parameters during its execution.  The special  parameter  #
       is  updated  to reflect the change.  Special parameter 0 is unchanged.  The first element of the FUNCNAME vari‐
       able is set to the name of the function while the function is executing.