Search code examples
zsh

zsh - bad substitution for echo


on macOS with it's zsh - the below script (snippet) did work with bash. I have two text files, the first one that keeps a variable of menu file we are working with now, the other one, that can be mane of them - showing the menu text (a1=Some menu description item) , and the command line (c1=Some command to run).

The following shell script:

CommandDesc="a$1"       
CommandString="c$1"        #  The Command string     ie. cmd3

case "$1" in

"")
    for (( i=0; i<=$ans_menu_count; i++ ))
        do
            #eval var1="ans"$i"_opt"    #creating a var ans1_opt
            eval desc="a"$i             #Creating a var ans1_desc
            #echo "${!var1} : ${!var2}"
            echo "$i : ${!desc}"
        done ;

(The full script can be found here, above just a snippet: https://github.com/IoTPlay/menu_ansible/tree/master/ans-menu)

the above script, working with bash, produces an error bad substitution at the line echo "$i : ${!desc}" with zsh.

I read the other articles - but cannot see how to fix it. Any guidance please?


Solution

  • Got it working, thank you, main culprit was making a "variable from a variable" like so:

    ${(P)CommandString}

    and a few small other thingies that zsh does differently than bash.

    Full working menu system here: https://github.com/IoTPlay/menu_ansible/blob/master/ans-menu/ans.zsh