#!/bin/bash SUNDAY_MENU=BREAD MONDAY_MENU=APPLES TODAY=MONDAY ECHO "I want ${${TODAY}_MENU}" # does not work, bad substitution ECHO "I want ${`echo $TODAY`_MENU}" # does not work, bad substitution
Any Ideas ?
Use variable indirection like this:
varname=${TODAY}_MENU
echo ${!varname}
If you are using Bash 4 or later, however, you are probably better off using an associative array :
menu=([sunday]=bread [monday]=apples)
echo ${menu[$TODAY]}
I use eval function
#!/bin/bash
SUNDAY_MENU=BREAD
MONDAY_MENU=APPLES
TODAY=MONDAY
eval TODAY_MENU=\$\{${TODAY}_MENU\}
echo "I want ${TODAY_MENU}"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.