set -- 后无内容,将当前 shell 脚本的参数置空,$1 $? $@ 等都为空。
set -- 后有内容,当前 shell 脚本的参数被替换为 set -- 后的内容,$1 $? $@ 等相应地被改变。
举例
//文件 tmp.sh
#!/bin/sh
#set -- aa ss dd
echo $0 #命令行
echo $1 #第一个参数
echo $* #所有参数
echo $@ #所有参数
echo $# #命令行参数个数
执行./tmp.sh qq ww ee的输出
./tmp.sh
qq ww ee
qq ww ee
3
去掉set --前的注释#,执行./tmp.sh qq ww ee的输出
./tmp.sh
aa
aa ss dd
aa ss dd
3
一点线索
mysql 的 DOCKFILE
在看 mysql 的 DOCKFILE 怎么写时,也看到了相同的写法如下:
mysql/5.7/docker-entrypoint.sh
# if command starts with an option, prepend mysqld
if [ "${1:0:1}" = '-' ]; then
set -- mysqld "$@"
fi
ps: ${1:0:1} =》 ${parameter:offset:length}
set 的 man page
查到 set 的 man page 中有对 -- 有如下介绍
set [--abefhkmnptuvxBCEHPT] [-o option-name] [arg ...]
set [+abefhkmnptuvxBCEHPT] [+o option-name] [arg ...]
-- If no arguments follow this option, then the positional parameters are unset. Otherwise, the positional parameters are set to the args, even if some of them begin with a -.
名词解释
Positional parameters in a shell script are nothing but the command line arguments passed to a shell script.
它啥也不是,就是指的 shell 脚本的参数