You are not logged in.
I am trying to set variables by $var=$var in the following nested for loops.
It does not work. Is there anything I can do like that?
#!/bin/bash
aa="bb cc dd ee ff gg"
for i in $aa; do
for ii in BBVAR CCVAR DDVAR EEVAR FFVAR GGVAR; do
# I am trying to set variable like BBVAR=bb for example.
$ii=$i
done
done
echo "$BBVAR" # expecting "bb" here
echo "$CCVAR" # expecting "cc" here
echo "$DDVAR" # expecting "dd" here
echo "$EEVAR" # expecting "ee" here
echo "$FFVAR" # ecpecting "ff" here
echo "$GGVAR" # expecting "gg" here
Last edited by duyinthee (2023-09-21 08:28:27)
Offline
Like this?
var1="hold"
declare $var1="told"
echo $hold
Offline
Do you really want explicit variables for those indexed values? Why not:
#!/bin/bash
aa="bb cc dd ee ff gg"
set -- $aa
echo $1 # outputs "bb"
echo $2 # outputs "cc"
echo $3 # outputs "dd"
Offline
I suspect it's not about $aa, but a general question about dynamically named variables.
Offline
let me paste here my solution for what I am trying. But I want to combine those if...else...fi parts and shorten.
That's why, I am considering for i in; do... done
#!/bin/bash
aa="bb cc dd ee ff gg"
num=1
for i in $aa; do
# for ii in BBVAR CCVAR DDVAR EEVAR FFVAR GGVAR; do
# declare "$ii=$i"
# done
if [[ $((num)) -eq 1 ]]; then
BBVAR=$i
num=$((num+1))
continue
fi
if [[ $((num)) -eq 2 ]]; then
CCVAR=$i
num=$((num+1))
continue
fi
if [[ $((num)) -eq 3 ]]; then
DDVAR=$i
num=$((num+1))
continue
fi
if [[ $((num)) -eq 4 ]]; then
EEVAR=$i
num=$((num+1))
continue
fi
if [[ $((num)) -eq 5 ]]; then
FFVAR=$i
num=$((num+1))
continue
fi
if [[ $((num)) -eq 6 ]]; then
GGVAR=$i
num=$((num+1))
continue
fi
done
echo "$BBVAR"
echo "$CCVAR"
echo "$DDVAR"
echo "$EEVAR"
echo "$FFVAR"
echo "$GGVAR"
What I really want is to set many variables based on a command's outputs.
Last edited by duyinthee (2023-09-19 08:50:31)
Offline
Have you tried the declare thing I posted?
EDIT: I also recommend having a look at "associative array" in bash, like https://linuxhint.com/associative_array_bash/
Last edited by Awebb (2023-09-19 09:56:42)
Offline
Replacing:
$ii=$i
with:
eval "$ii=$i"
will assign a value to all of the xxVAR variables but they will all be assigned the value gg. You have nested loops and the eval statement will be executed 36 times. Each xxVAR variable will be assigned each of the aa values consecutively. The following would work:
#!/bin/bash
declare -A assArray=( [BBVAR]=bb
[CCVAR]=cc
[DDVAR]=dd
[EEVAR]=ee
[FFVAR]=ff
[GGVAR]=gg )
for val in "${!assArray[@]}"; do
eval "$val=${assArray[$val]}"
done
echo "$BBVAR" # expecting "bb" here
echo "$CCVAR" # expecting "cc" here
echo "$DDVAR" # expecting "dd" here
echo "$EEVAR" # expecting "ee" here
echo "$FFVAR" # ecpecting "ff" here
echo "$GGVAR" # expecting "gg" here
Last edited by eda2z (2023-09-19 17:56:05)
Offline
Thanks you all for replies with suggestions. Yes, it works with "declare" and also "eval".
What I do now is this below;
#!/bin/bash
aa="bb cc dd ee ff gg"
IFS=' '
NUM=1
for i in $aa; do
ii=$(echo "BB CC DD EE FF GG" | cut -d ' ' -f "$NUM")
eval $i=$ii # here (declare $i=$ii) is also work
NUM=$((NUM+1))
done
echo $bb
echo $cc
echo $dd
echo $ee
echo $ff
echo $gg
I don't know declare or eval is the right one here.
Offline