I need to replace temporary stored files by variables. I call 6 files from a bitbucket repo and want to place each file content in an own variable.
# declare array for all files
declare -a file_array=("one.txt" "two.txt" "three.txt" "four.json" "five.txt" "six.txt")
for file in "${file_array[@]}"
do
echo "$file"
"file_$file='$(curl -k -s -X GET -H "${var1}" -H "${var2}" "${var3}${var4}")'"
done
This code just gives me an error. It seems like it is always taking the content of the file as a command:
$'file_one.txt=\'<<file content>>'': command not found
You have at least two problems here: you try to put a dot in a variable name, which is invalid, and you try to assign expressions. Plus your quoting which also looks strange.
You could use namerefs (declare -n
) and substitute the dots, e.g., with underscores:
# declare array for all files
declare -a file_array=("one.txt" "two.txt" ...)
for file in "${file_array[@]}"; do
echo "$file"
declare -n f="file_${file/./_}"
f=$(curl -k -s -X GET -H "${var1}" -H "${var2}" "${var3}${var4}")
done
printf '%s\n' "$file_one_txt"