nameref
A bash 4.3 variable declared with declare -n or local -n that acts as another name for a variable named by its value.
In more detail
Assigning to the nameref assigns to the variable it refers to, so a function can write its result into a variable the caller names, without eval. ShellCheck cannot follow the link and may report SC2034 for it.
Example
read_config() { local -n target="$1"; target="ledger.example.com"; }Related terms
- indirect expansion — The bash form ${!name}, which reads the value of the variable whose name is stored in name.
- declare — The bash builtin that creates variables with attributes: -i integer, -r read-only, -a array, -A associative array, -n name reference, and -p to print.
- eval — A shell builtin that joins its arguments into one string and runs that string as a command, as if you had typed it.
- SC2034 — The ShellCheck warning for a variable that is assigned but never read in the file.
Taught in
- eval, And What To Use Instead — Bash Scripting & Command Line Automation