I want to define the following:
COMMIT_ID_${ARCH}=$(git log --format="%H" -n 1)
But it fails:
-bash: COMMIT_ID_amd64=a7c9e0a53972a3d42a8035f45469f1959a0475f8: command not found
Use an associative array.
declare -A commit_id
commit_id[$ARCH]=$(git log --format="%H" -n 1)
...
echo "${commit_id[$ARCH]}"
-g flag to the declare command to make the array a global variable.You can use declare directive for this:
declare "COMMIT_ID_${ARCH}"=$(git log --format="%H" -n 1)
To examine new variable use:
declare -p "COMMIT_ID_${ARCH}"
Based on comments below by OP:
# create your variable
var="COMMIT_ID_${ARCH}"
# set variable by calling git log
declare "$var"=$(git log --format="%H" -n 1)
# examine value of $var
echo "${!var}"
COMMIT_ID_amd64 and COMMIT_ID_amd64v8 after above run.var is just a temporary holder. Where and how are you going to use these 2 variables?
COMMIT_ID_x86andCOMMIT_ID_arm, there's nothing gained by definingCOMMIT_ID_x86instead of simplyCOMMIT_ID.