Indirect and transformation expansions
How it works
${!name} can read a variable whose name is stored in another ordinary scalar variable. For example, if name contains theme, the result comes from theme. Use only fixed trusted variable names; untrusted names can select unintended variables or introduce evaluation through array subscripts. ${!PREFIX@} lists names beginning with a fixed prefix as separate quoted arguments, while ${!PREFIX*} joins them in double quotes using the first IFS character, a space when IFS is unset, or no separator when it is empty. This is different from reading their values. Modern Bash transformation forms ${p@U}, ${p@u}, ${p@L} convert case; ${p@Q} gives a shell-quoted representation, not a general sanitizer or a reason to use eval. ${p@E} interprets backslash escapes, ${p@A} represents an assignment/declaration and ${p@a} exposes attributes. Other forms, including prompt and array-oriented transformations, require additional context and are left for advanced study. Examples target Bash 5.2; older versions may lack these operators.
Run each example separately in a noninteractive Bash 5.2 script in a fresh empty practice directory, with default options and C locale. Output is shown separately; do not enter it as code.
Read a trusted variable name
Expected stdout:
themedarkOrdinary expansion prints the name; indirect expansion looks up its value.
Quoted representation and case
p='daily report'printf '%s' "${p@Q}" "${p@U}" "${p@u}" "${p@L}"Expected stdout:
'daily report'DAILY REPORTDaily reportdaily reportThese are textual representations and transformations, not commands to run.
List known names
BM6_ALPHA=oneBM6_BETA=twoIFS=: printf '[%s]' "${!BM6_@}" "${!BM6_*}"Expected stdout:
[BM6_ALPHA][BM6_BETA][BM6_ALPHA:BM6_BETA]The @ form supplies separate arguments; the * form joins names. The example uses a clean Bash 5.2 environment and fixed names.
Common mistakes
Do not turn user input into arbitrary variable names or execute the output of a transformation. For two known variables a direct reference is often clearer. Nameref and array-specific cases are outside this beginner lesson.
Practice
Create theme=dark and name=theme. Print the indirect value, then print a quoted representation of the literal string daily report. Explain why you should not execute that representation.
Next: Input, output and exit status