Artificial intelligent assistant

bash dynamic (variable) variable names I want to dynamically create a sequence of strings by manipulate an array of elements and create some arithmetic procedure. for name in FIRST SECOND THIRD FOURTH FIFTH; do $name = $(( $6 + 1 )) $name = "${$name}q;d" echo "${$name}"; printf "\n" done The desire outcome would be the below for `$6` equals `0`. 1q;d 2q;d 3q;d 4q;d 5q;d But I get this error reel_first_part.sh: line 18: FIRST: command not found reel_first_part.sh: line 19: ${$name}q;d: bad substitution reel_first_part.sh: line 18: FIRST: command not found reel_first_part.sh: line 19: ${$name}q;d: bad substitution reel_first_part.sh: line 18: FIRST: command not found reel_first_part.sh: line 19: ${$name}q;d: bad substitution I guess it's something simple. It used to work when I did something like FIRST=$(( $6 + 1 )) FIRST="${FIRST}q;d"

First of all there can not be any space around `=` in variable declaration in `bash`.

To get what you want you can use `eval`.

For example a sample script like yours :


#!/bin/bash
i=0
for name in FIRST SECOND THIRD FOURTH FIFTH; do
eval "$name"="'$(( $i + 1 ))q;d'"
printf '%s\
' "${!name}"
i=$(( $i + 1 ))
done


Prints :


1q;d
2q;d
3q;d
4q;d
5q;d


Use `eval` cautiously, some people call it evil for some valid reason.

`declare` would work too :


#!/bin/bash
i=0
for name in FIRST SECOND THIRD FOURTH FIFTH; do
declare "$name"="$(( $i + 1 ))q;d"
printf '%s\
' "${!name}"
i=$(( $i + 1 ))
done


also prints :


1q;d
2q;d
3q;d
4q;d
5q;d

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy d6fd3f3562cf0f2a6347b4c17c6edf1c