Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add to an associative array in zsh?

Tags:

zsh

I'm trying to change an element of a zsh associative array, but I can't find any clues on the correct syntax.

The PHP equivalent would be

$assoc['key'] = 'newvalue';

but how can I do this in zsh?

The documentation seems to be very confusing on this, as it assumes that once you've set up an associative array, you never want to change it.

EDIT: this is what I'm trying to do

% noglob ZSH_HIGHLIGHT_STYLES[globbing]='fg=yellow' zsh: command not found: ZSH_HIGHLIGHT_STYLES[globbing]=fg=yellow

$ZSH_HIGHLIGHT_STYLES is defined by the zsh-syntax-highlighting plugin.

like image 869
John Y Avatar asked May 23 '16 09:05

John Y


People also ask

Does zsh support associative arrays?

One of the advantages of zsh over bash 3 is the support of “associative arrays,” a data structure known as hash tables or dictionaries in other languages.

Which is better bash or zsh?

Zsh is built on top of bash thus it has additional features. Zsh is the default shell for macOS and Kali Linux. Zsh provides the user with more flexibility by providing various features such as plug-in support, better customization, theme support, spelling correction, etc.

Do Bash arrays start at 0 or 1?

Bash indexed arrays are zero based, this means that to access the first element we have to use the index zero.


1 Answers

It's actually pretty simple, assuming you have an associative array.

typeset -A assoc
assoc[key]=newvalue
like image 177
chepner Avatar answered Sep 30 '22 12:09

chepner