You are not logged in.

#1 2023-09-19 03:54:09

duyinthee
Member
Registered: 2015-06-14
Posts: 213
Website

[SOLVED] can I set variables by $var=$var in nested for loops?

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

#2 2023-09-19 05:51:16

Awebb
Member
Registered: 2010-05-06
Posts: 6,174

Re: [SOLVED] can I set variables by $var=$var in nested for loops?

Like this?

var1="hold"
declare $var1="told"
echo $hold

Offline

#3 2023-09-19 06:11:19

bulletmark
Member
From: Brisbane, Australia
Registered: 2013-10-22
Posts: 634

Re: [SOLVED] can I set variables by $var=$var in nested for loops?

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

#4 2023-09-19 06:21:08

Awebb
Member
Registered: 2010-05-06
Posts: 6,174

Re: [SOLVED] can I set variables by $var=$var in nested for loops?

I suspect it's not about $aa, but a general question about dynamically named variables.

Offline

#5 2023-09-19 08:36:50

duyinthee
Member
Registered: 2015-06-14
Posts: 213
Website

Re: [SOLVED] can I set variables by $var=$var in nested for loops?

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

#6 2023-09-19 09:55:47

Awebb
Member
Registered: 2010-05-06
Posts: 6,174

Re: [SOLVED] can I set variables by $var=$var in nested for loops?

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

#7 2023-09-19 17:35:36

eda2z
Member
From: Woodstock, IL
Registered: 2015-04-21
Posts: 64

Re: [SOLVED] can I set variables by $var=$var in nested for loops?

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

#8 2023-09-21 08:12:03

duyinthee
Member
Registered: 2015-06-14
Posts: 213
Website

Re: [SOLVED] can I set variables by $var=$var in nested for loops?

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

Board footer

Powered by FluxBB