0

I have an array and i am looping through the variable to run other commands. The last thing i need to do with the array value before going onto the next one is create a variable with the array value as the name and assign a string that includes other variables inside.

Currently my code looks like this:

for i in ${array[@]} do
    some command
    $i_value="my string including other variables ${other_variable}"
done
echo $i_value

when i run this i get the following error and i dont know why its looking for a file and not creating a variable with the array value (which its getting):

line 5: *value*_value=my string including other variables *other_variable*: no such file or directory.
3

If you don't have an external need to create variables foo_value, bar_value etc., but only need them within the script, you should probably use an associative array instead. Like so:

declare -A values
for i in "${array[@]}"; do
    # some commands...
    values[$i]="my string including other variables ${other_variable}"
done
echo "${values[$i]}"

# loop over the keys:
for key in "${!values[@]}"; do
    echo "values has key '$key'"
done

(Note that you want to put quotes around "${array[@]}" to deal with values containing whitespace and glob characters correctly; and you need to have a semicolon before do in for i in ...; do.)

| improve this answer | |
  • I think this is going to be the answer but i have a couple issues. when i get out of the first for loop i want to loop through the keys and for each key and want to echo all the values associated to that key. using for key in !values gets me each key but i cant list all the values associated to the key? can one key only have one value? – a.smith Oct 1 '19 at 9:36
  • @a.smith, yep, there's only one value per each key. Bash can't really do nested data structure, arrays-in-arrays or arrays-in-associative-arrays. I think (some versions of) ksh maybe can, but I'm not sure and don't remember the syntax. You could work around with delimited strings and word splitting, but that's annoying, and, in my opinion, it would probably be better to consider using some real programming language instead... – ilkkachu Oct 1 '19 at 9:55
  • Ok thanks, i can admit defeat and try this in a different language. I have written 90% of the bash script already and in my script i am executing certain cmd line commands. Do you have a suggestion of the best language i should use given its mostly already written in bash? – a.smith Oct 1 '19 at 10:46
  • @a.smith, that really depends on what languages you know. I suppose Python is popular, but really it's up to you. – ilkkachu Oct 1 '19 at 11:15
0

Just use export builtin ...

Example (with bonus: use of dynamic variable name for accessing ;-p)

~$ cat test.sh 
#!/bin/bash

toto="coincoin"
tata="cuicui"

for var in toto tata; do
    export coincoin_${!var}="${var}"
done

echo "${coincoin_cuicui}"
echo "${coincoin_coincoin}"

~$ ./test.sh 
tata
toto
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.