0
$ declare -p ar
declare -a ar='()'


$ declare -p mmm
declare -- mmm="hello"

What does -- mean here? Does it tell some attribute of mmm?

Thanks.

Tim
  • 101,790

3 Answers3

2

It's just there for consistency of output format; the first field is the literal declare; the second field is the attribute list; the third field is the var=value.

So for a variable with no specific attributes the command needs to have a way of showing "no attributes"; this is done with --

eg

bash-4.2$ declare y=100
bash-4.2$ declare -p y
declare -- y="100"
bash-4.2$ declare -l y
bash-4.2$ declare -p y
declare -l y="100"
bash-4.2$ declare +l y
bash-4.2$ declare -p y
declare -- y="100"

The result can be stored in a file and sourced in later.

1

The double dash -- declares the end of options for the given command. You'll find a very smart description by cuonglm by following this link.

The given example shows how to grep for the value -v like this:

grep -- -v inputFile

So -v does not trigger the --invert-match option, but greps for the string -v inside the inputFile.

ChristophS
  • 565
  • 5
  • 13
1

-- marks the end of options. It's superfluous here (since the variable name won't start with - (except for the pathological cases where the user's locale would be defined with - in the alpha character class) and anyway it's not added when there are flags) but harmless. I suppose it's there because the code had:

printf ("declare -%s ", i ? flags : "-");

Which was quicker to type than:

if (i)
  printf ("declare -%s ", i ? flags : "-");
else
  printf ("declare ");

Ironically, in bash-2.01, that code was changed to:

if (pattr == 0 || posixly_correct == 0)
  printf ("declare -%s ", i ? flags : "-");
else if (i)
  printf ("%s -%s ", this_command_name, flags);
else
  printf ("%s ", this_command_name);

To be reused for export and readonly, where that time the -- was avoided for export var=value but not declare -- var=value.