In man bash it is mentioned that set
has two options -
and --
I was wondering if there is any difference while using -
and --
as options to set
while setting positional parameters.
I could not find any big difference mentioned in man bash when it comes to their usage in setting positi开发者_运维技巧onal params.
You use --
(double minus) to indicate the end of shell options and the start of arguments. Hence, for example:
set -- -a -b -f somefile
After this, $1
is -a
, $2
is -b
, $3
is -f
and $4
is somefile
.
Without the --
, the shell would interpret -a
, -b
and -f
as shell options, and would set $1
to somefile
.
The Bash (4.1) manual says (of set
):
--
If no arguments follow this option, then the positional parameters are unset. Otherwise, the positional parameters are set to the arguments, even if some of them begin with a ‘-
’.
-
Signal the end of options, cause all remaining arguments to be assigned to the positional parameters. The ‘-x
’ and ‘-v
’ options are turned off. If there are no arguments, the positional parameters remain unchanged.
The bash(1) man page for 4.1.5(1) says:
-- If no arguments follow this option, then the positional
parameters are unset. Otherwise, the positional parame‐
ters are set to the args, even if some of them begin
with a -.
- Signal the end of options, cause all remaining args to
be assigned to the positional parameters. The -x and -v
options are turned off. If there are no args, the posi‐
tional parameters remain unchanged.
The first difference is when there are no arguments after the -
or --
. For the former, the existing positional parameters will be unchanged. For the latter, the positional parameters will be cleared.
So set --
clears the positional parameters and set -
is a no-op.
The -v
and -x
settings may be modified by set - ...
. So, if you had set -v
turned on (which causes the shell to print input lines as they are read), it will be turned off by the set - ...
command. set -- ...
will leave it unchanged.
set -x
is more common that set -v
in that set -x
is often used to debug scripts to see exactly what commands are being run. Quite often when debugging a shell script, you would run it with bash -x <script>
. Knowing that set - ...
turns -x
off, you'd probably want to use set -- ...
, since it would be quite unexpected to have -x
turned off as a side effect of another command.
The convention is to use a single -
for single-letter arguments, e.g. -i
and double --
for their full-worded counterpart, e.g. --interactive
.
See here for more information.
This has been answered very well elsewhere on the StackExchange network.
UPDATE
The double-dash --
prefixes long (verbatim) options to commands, but when used with a Bash builtin (like set
), it means the end of options to that particular command.
E.g. if you want to create a file that starts with a dash:
touch -dashed
...you'll get an error:
touch: illegal option -- h
usage: touch [-acfm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file ...
However, try it with --
, et voila:
touch -- -dashed
...and then ls
to see -dashed
in your current directory.
See here for more information.
In my bash
manpage there is a sentence: An argument of - is equivalent to --
.
精彩评论