4

I want to export a variable from within a loop. I was not able to do it. I am not sure what is missing here.

Any suggestion would be great help

var="ahs tcs amq tos"

for i in ${var}
do
   ${i}_log="/var/tmp/pmp_${i}_log"
   #export ${i}_log
done
1
  • Is $i the value you expect it to be? Perhaps you should echo it to make sure. Commented Jun 8, 2017 at 7:36

4 Answers 4

3

The idea is right, just use the declare variable to create variables on the fly. Also avoid using un-quoted variable expansion(for i in ${var}) for looping. Use a proper array syntax as

var=("ahs" "tcs" "amq" "tos")

for i in "${var[@]}"; do
    declare ${i}_log="/var/tmp/pmp_${i}_log"
    export "${i}_log"
done

As a side note for good practice, always specify the interpreter to run your script. It could be #!/bin/bash or #!/bin/sh or best do it by #!/usr/bin/env bash

Sign up to request clarification or add additional context in comments.

Comments

0

This works on dash and bash (as long as the i's and the path to interpolate them into are reasonable):

#!/bin/sh
var="a b c d"

for i in $var
do
        export "${i}_log=/var/tmp/pmp_${i}_log"


        #test
        sh -c "echo \$${i}_log"
done

2 Comments

I tried this and its giving me the expected..Result...thanks for help
@SunilSharma I don't need thanks :D Gimme your upvotes and accepts! :D
0

An alternative could be to use a single exported associative array instead of multiple variables:

EDIT: oops, this won't work since arrays can't be exported. :\

var="ahs tcs amq tos"

declare -A logs

for i in ${var}
do
  logs[$i]="/var/tmp/pmp_${i}_log"
done

echo ${logs[@]}
#### export logs

Also see Inian's answer for better practices for looping and arrays.

4 Comments

Unfortunately, arrays can't be exported
@Mike oh right... thanks. TIL Doesn't help that bash doesn't throw any errors when I try to export it.
@Mike why would you say arrays cant be exported? Furthermore you can even export a newline or space separated list (depends on your $IFS) and parse it later with real_list=($seperated_list) You can then retrieve values by first_var=${real_list[0]}
@Mike you can export arrays with export some_list=("${thelist[@]}") The main precaution to take is that if your IFS field seperator is a space for example, you may get incorrect data when parsing back into a list.
0

This can be done in one line without a loop

printf '%s\n' {ahs,tcs,amq,tos} | xargs -I {} bash -c 'export {}_log="/var/tmp/pmp_{}_log"; echo {}_log=${{}_log}'

or with a loop

#!/bin/bash
for i in {ahs,tcs,amq,tos}; do 
    #export
    export "${i}_log=/var/tmp/pmp_${i}_log"; 

    #test
    bash -c 'echo '"${i}_log"'='"\$${i}_log"; done
done

The reason ${i}_log="/var/tmp/pmp_${i}_log" failed is because ${i}_log is unquoted and the syntax for exporting is export somevar=somedefintion. In order to dynamically generate the variable name, surround the statement in quotes so that it gets interpolated. ie. export "${dynamic}_var=${dynamic}_definition"

see http://wiki.bash-hackers.org/syntax/quoting

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.