3

I can give the -U attribute to a non-associative array so that it can't contain duplicate elements:

% declare -U path
% path=( ~/bin $path )
% path=( ~/bin $path )
% echo $path

Here, even though I've added ~/bin to the array path twice, the latter contains only one ~/bin element.

I'm looking for a way to print all the attributes which can be given to a parameter during an assignment, including this -U attribute.

I've looked at the typeset command in man zshbuiltins, and found this excerpt:

Except when assignments are made with name=value, using +m forces the matching parameters and their attributes to be printed, even inside a function.

So, I've tried executing these commands:

% declare -U path
% declare +m 'path'

Whose output is:

array path

While it tells me the type of the parameter (here an array), it doesn't tell me that it has the -U attribute.

Is there another command which would print all the attributes of a parameter?

3

You can use:

$ echo ${(t)path}
array-unique-special

I'd expect typeset -p path (or declare; I prefer typeset as it's more portable) to give that information. It doesn't though in current versions which seems like a bug to me (Edit: now fixed).

You can also use:

typeset -U +

To list the variables with the unique attribute.

  • Thank you very much for fixing the bug. declare -p path prints typeset -a path=(...), but it doesn't report that path is a tied array (created with the -T option). Shouldn't it print typeset -a -T PATH=... path=(...)? OTOH, echo ${(t)path} prints array-special. I suppose special means the array is tied to PATH. Also, declare -T doesn't print anything, while I have 10 tied arrays (cdpath, fignore, ...). Is it supposed to ignore the tied arrays created automatically by the shell? – user938271 Sep 26 '18 at 10:24
  • Ah, typeset -a -T PATH=... path=(...) is not a valid command. It should have been typeset -T PATH path. So typeset -p must choose between reporting that the array is tied, or giving its value. It can't do both at the same time... – user938271 Sep 26 '18 at 10:33
  • 1
    No special is because $PATH is special to the shell. After typeset -T A a, I see a tag_local which is wrong as tag_local refers to a function (not variable) property. It should show tied there. – Stéphane Chazelas Sep 26 '18 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.