Avatar of ltpitt
ltpitt
 asked on

How can I unset a key / value pair in a BASH associative array?

Hi all, I have this code:
declare -A CONSOLES=(
                     ["/home/pi"]="local"
                     ["pi@192.168.178.170:/home/pi"]="pitendo"
                     ["ftp://192.168.178.171/sd/retroarch"]="nintendowii"
                     ["ftp://192.168.178.172:1337/ux0:/data/retroarch"]="psvita"
                    )




unset $CONSOLES[local]
unset ${CONSOLES[/home/pi]}


echo "${!CONSOLES[@]}"

Open in new window

But I am not able to remove from CONSOLES the key / value pair 
["/home/pi"]="local"

Open in new window


Can you help?

Thanks.
* BashProgramming

Avatar of undefined
Last Comment
noci

8/22/2022 - Mon
Andrei Fomitchev

It should work:
unset CONSOLES["/home/pi"] 

Open in new window


noci

Some additional info:
Using unset $....   means unsetting the expansion of some variables, while you need access to the variable itself.
( bash != perl )


ltpitt

ASKER
Hello!

Sadly it does not work:
declare -A CONSOLES=(
                     ["/home/pi"]="local"
                     ["pi@192.168.178.170:/home/pi"]="pitendo"
                     ["ftp://192.168.178.171/sd/retroarch"]="nintendowii"
                     ["ftp://192.168.178.172:1337/ux0:/data/retroarch"]="psvita"
                    )
echo "Before unset:"
echo "${!CONSOLES[@]}"
echo "After unset:"
unset ${CONSOLES["/home/pi"]}
echo "${!CONSOLES[@]}"

Open in new window



Output:
Before unset:
ftp://192.168.178.171/sd/retroarch /home/pi pi@192.168.178.170:/home/pi ftp://192.168.178.172:1337/ux0:/data/retroarch
After unset:
ftp://192.168.178.171/sd/retroarch /home/pi pi@192.168.178.170:/home/pi ftp://192.168.178.172:1337/ux0:/data/retroarch

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
noci

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ltpitt

ASKER
Sorry I really missed that!
Thanks for your help.
noci

No problem...  answers on EE mostly are relevant.  They may contain typo's etc. and need correction sometimes.