Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

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?

question from:https://stackoverflow.com/questions/65876476/zsh-bad-substitution-for-echo

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...