Skip to main content
added 830 characters in body
Source Link
Stéphane Chazelas
  • 587.9k
  • 96
  • 1.1k
  • 1.7k

Another option to match on the keys of the $parameter special associative array is to use the I subscript flag:

$ print -rC1 - $parameters[(I)ba*] bar baz 

That's an associative array operator which results in list/array, while @Gille's ${(kM)parameters:#ba*} is a list/array operator applied to the keys of the associative array also resulting in a list/array. The end result is essentially the same in this case here.

If you have to remember only one, the ${(M)array:#pattern} which is grep-for-arrays is the more useful one as it's more generic. The things it can't do compared to array subscript flags is match on keys and return the corresponding values or match on values and return the corresponding keys.

You can combine both like in:

$ print -rC1 - ${(Mk)parameters[(R)array*]:#pa*} path patchars 

Here to return the names of arrays that start with pa (values of $parameters are matched against array* and keys against pa*).

Of course, you can do that for all of zsh's introspection variables ($aliases, $commands, $functions, $builtins, $modules, $history...)

For some of them, you can also query using a -m pattern (match) option of the corresponding builtin (whence -m, alias -m, autoload -m, fc -m...), but like for bash's compgen, you can't reliably post-process their output as it's line-based while some of the things they report may contain newline characters.

Another option to match on the keys of the $parameter special associative array is to use the I subscript flag:

$ print -rC1 - $parameters[(I)ba*] bar baz 

Of course, you can do that for all of zsh's introspection variables ($aliases, $commands, $functions, $builtins, $modules, $history...)

For some of them, you can also query using a -m pattern (match) option of the corresponding builtin (whence -m, alias -m, autoload -m, fc -m...), but like for bash's compgen, you can't reliably post-process their output as it's line-based while some of the things they report may contain newline characters.

Another option to match on the keys of the $parameter special associative array is to use the I subscript flag:

$ print -rC1 - $parameters[(I)ba*] bar baz 

That's an associative array operator which results in list/array, while @Gille's ${(kM)parameters:#ba*} is a list/array operator applied to the keys of the associative array also resulting in a list/array. The end result is essentially the same in this case here.

If you have to remember only one, the ${(M)array:#pattern} which is grep-for-arrays is the more useful one as it's more generic. The things it can't do compared to array subscript flags is match on keys and return the corresponding values or match on values and return the corresponding keys.

You can combine both like in:

$ print -rC1 - ${(Mk)parameters[(R)array*]:#pa*} path patchars 

Here to return the names of arrays that start with pa (values of $parameters are matched against array* and keys against pa*).

Of course, you can do that for all of zsh's introspection variables ($aliases, $commands, $functions, $builtins, $modules, $history...)

For some of them, you can also query using a -m pattern (match) option of the corresponding builtin (whence -m, alias -m, autoload -m, fc -m...), but like for bash's compgen, you can't reliably post-process their output as it's line-based while some of the things they report may contain newline characters.

added 11 characters in body
Source Link
Stéphane Chazelas
  • 587.9k
  • 96
  • 1.1k
  • 1.7k

Another option to match on the keys of the $parameter special associative array is to use the I subscript flag:

$ print -rC1 - $parameters[(I)ba*] bar baz 

Of course, you can do that for all of zsh's introspection variables ($aliases, $commands, $functions, $builtins, $modules, $history...)

For some of them, you can also query using a -m pattern (match) option of the corresponding builtin (whence -m, alias -m, autoload -m, fc -m...), but like for bash's compgen, you can't reliably post-process their output as it's line-based while some of the things they report may contain newline characters.

Another option to match on the keys of the $parameter special associative array is to use the I subscript flag:

$ print -rC1 - $parameters[(I)ba*] bar baz 

Of you can do that for all of zsh's introspection variables ($aliases, $commands, $functions, $builtins, $modules, $history...)

For some of them, you can also query using a -m pattern (match) option of the corresponding builtin (whence -m, alias -m, autoload -m, fc -m), but like for bash's compgen, you can't reliably post-process their output as it's line-based while some of the things they report may contain newline characters.

Another option to match on the keys of the $parameter special associative array is to use the I subscript flag:

$ print -rC1 - $parameters[(I)ba*] bar baz 

Of course, you can do that for all of zsh's introspection variables ($aliases, $commands, $functions, $builtins, $modules, $history...)

For some of them, you can also query using a -m pattern (match) option of the corresponding builtin (whence -m, alias -m, autoload -m, fc -m...), but like for bash's compgen, you can't reliably post-process their output as it's line-based while some of the things they report may contain newline characters.

added 467 characters in body
Source Link
Stéphane Chazelas
  • 587.9k
  • 96
  • 1.1k
  • 1.7k

Another option to match on the keys of the $parameter special associative array is to use the I subscript flag:

$ print -rC1 - $parameters[(I)ba*] bar baz 

Of you can do that for all of zsh's introspection variables ($aliases, $commands, $functions, $builtins, $modules, $history...)

For some of them, you can also query using a -m pattern (match) option of the corresponding builtin (whence -m, alias -m, autoload -m, fc -m), but like for bash's compgen, you can't reliably post-process their output as it's line-based while some of the things they report may contain newline characters.

Another option to match on the keys of the $parameter special associative array is to use the I subscript flag:

$ print -rC1 - $parameters[(I)ba*] bar baz 

Another option to match on the keys of the $parameter special associative array is to use the I subscript flag:

$ print -rC1 - $parameters[(I)ba*] bar baz 

Of you can do that for all of zsh's introspection variables ($aliases, $commands, $functions, $builtins, $modules, $history...)

For some of them, you can also query using a -m pattern (match) option of the corresponding builtin (whence -m, alias -m, autoload -m, fc -m), but like for bash's compgen, you can't reliably post-process their output as it's line-based while some of the things they report may contain newline characters.

Source Link
Stéphane Chazelas
  • 587.9k
  • 96
  • 1.1k
  • 1.7k
Loading