Set Positional Parameters
範例 ex_001
ex_001.sh
#!/usr/bin/env bash
set - aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo
echo '=============================================='
echo '$@:' $@
echo '$*:' $*
echo '=============================================='
echo '$#:' $#
echo '=============================================='
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}
echo '=============================================='
執行
$ ./ex_001.sh a b c d e f g h i j k l m n o
顯示
==============================================
$@: aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo
$*: aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo
==============================================
$#: 15
==============================================
$0: ./ex_001.sh
$1: aa
$2: bb
$3: cc
$4: dd
$5: ee
$6: ff
$7: gg
$8: hh
$9: ii
${10}: jj
${11}: kk
${12}: ll
${13}: mm
${14}: nn
${15}: oo
==============================================
範例 ex_002
ex_002.sh
#!/usr/bin/env bash
set -- aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo
echo '=============================================='
echo '$@:' $@
echo '$*:' $*
echo '=============================================='
echo '$#:' $#
echo '=============================================='
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}
echo '=============================================='
執行
$ ./ex_002.sh a b c d e f g h i j k l m n o
顯示
==============================================
$@: aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo
$*: aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo
==============================================
$#: 15
==============================================
$0: ./ex_002.sh
$1: aa
$2: bb
$3: cc
$4: dd
$5: ee
$6: ff
$7: gg
$8: hh
$9: ii
${10}: jj
${11}: kk
${12}: ll
${13}: mm
${14}: nn
${15}: oo
==============================================
範例 ex_003
ex_003.sh
#!/usr/bin/env bash
set -
echo '=============================================='
echo '$@:' $@
echo '$*:' $*
echo '=============================================='
echo '$#:' $#
echo '=============================================='
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}
echo '=============================================='
執行
$ ./ex_003.sh a b c d e f g h i j k l m n o
顯示
==============================================
$@: a b c d e f g h i j k l m n o
$*: a b c d e f g h i j k l m n o
==============================================
$#: 15
==============================================
$0: ./ex_003.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_004
ex_004.sh
#!/usr/bin/env bash
set --
echo '=============================================='
echo '$@:' $@
echo '$*:' $*
echo '=============================================='
echo '$#:' $#
echo '=============================================='
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}
echo '=============================================='
執行
$ ./ex_004.sh a b c d e f g h i j k l m n o
顯示
==============================================
$@:
$*:
==============================================
$#: 0
==============================================
$0: ./ex_004.sh
$1:
$2:
$3:
$4:
$5:
$6:
$7:
$8:
$9:
${10}:
${11}:
${12}:
${13}:
${14}:
${15}:
==============================================
文件說明
執行
$ help set
可以看到下面一段說明
set: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
Set or unset values of shell options and positional parameters.
Change the value of shell attributes and positional parameters, or
display the names and values of shell variables.
Options:
...略...
-- Assign any remaining arguments to the positional parameters.
If there are no remaining arguments, the positional parameters
are unset.
- Assign any remaining arguments to the positional parameters.
The -x and -v options are turned off.