# Assign environment variable to a variable's name

**URL:** <https://community.unix.com/t/assign-environment-variable-to-a-variables-name/397233>\
**Category:** Shell Programming and Scripting\
**Created:** [August 18, 2025, 7:00am UTC](https://community.unix.com/t/assign-environment-variable-to-a-variables-name/397233 "2025-08-18T07:00:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![overcraft](https://community.unix.com/letter_avatar/overcraft/32/6_449c533f586939b6e5f246632a46421f.png) [@overcraft](https://community.unix.com/u/overcraft)\
**Post date:** [August 18, 2025, 7:00am UTC](https://community.unix.com/t/assign-environment-variable-to-a-variables-name/397233/1 "2025-08-18T07:00:58Z")

</div>

Hi there,

I’m working on a pipeline (see _below_) where I defined a few environment variables. Also, I’m creating some on the way.

In particular, I have a `CONTIG` variable which I wish to make univocal for each sample (ID), haplotype (HAP), and chromosome (CHR\_P/M) combination; however, I’m not sure how to do it… simply defining `CONTIG` as `CONTIG_$ID_$h_$c` sounds wrong to me but I have never done this before, so I have no prior knowledge and could not find very helpful, succinct documentation on this.

This is the part of the pipeline of interest:

```bash
#!/bin/bash
#SBATCH --nodes=1 --ntasks=1 --cpus-per-task=12
#SBATCH --time=2-00:00:00
#SBATCH --mem=50gb
#
#SBATCH --job-name=name_test
#SBATCH --output=name_test.out
#SBATCH --array=[1-232]%1
#
#SBATCH --partition=DSS,NBFC

NAMES=$1
d=$(sed -n "$SLURM_ARRAY_TASK_ID"p $NAMES)

ID="$(echo ${d}/*_hap1_hprc_r2.fa.gz | sed 's#/path/to/hprc_y2_assemblies/[A-Z,0-9]\+_[a-z]/##' | sed 's#_hap1_hprc_r2.fa.gz##')"

HAP=( "hap1" "hap2")
SEX="$(echo ${d} | sed 's#/path/to/hprc_y2_assemblies/[A-Z,0-9]\+_##')"
CHR_P=("chr1" "chr2" "chr3" "chr4" "chr5" "chr6" "chr7" "chr8" "chr9" "chr10" "chr11" "chr12" "chr13" "chr14" "chr15" "chr16" "chr17" "chr18" "chr19" "chr20" "chr21" "chr22" "chrY" )
CHR_M=("chr1" "chr2" "chr3" "chr4" "chr5" "chr6" "chr7" "chr8" "chr9" "chr10" "chr11" "chr12" "chr13" "chr14" "chr15" "chr16" "chr17" "chr18" "chr19" "chr20" "chr21" "chr22" "chrX" )

BEDTOOLS="./tools/bedtools"
MINIMAP="./tools/minimap2"
SEQKIT="./tools/seqkit"
BGZIP="./tools/bgzip"

DNABRNN="./tools/dna-nn/dna-brnn"
MODELS="./tools/dna-nn/models/attcc-alpha.knm"

cd ~

#PREPARES THE INPUT
if [-e $d/select.done]
        then

                echo "regions selecion done!"

        else
                for h in "${HAP[@]}"; do
                        ${MINIMAP} -cx asm5 -t 12 CHM13v2.0.fna.gz $d/${ID}_${h}_hprc_r2.fa.gz > $d/${ID}_${h}.paf && \
                        if ["$SEX" == "m"]
                                then
                                if ["$HAP" == "hap1"]
                                        then
                                        for c in "${CHR_P[@]}"; do
                                                CONTIG=$(grep -w "${c}" $d/${ID}_${h}.paf | awk '{print $1}' | uniq) && \
                                                for s in "${CONTIG[@]}"; do
                                                ###generate chromosome FASTA
                                                        printf "%s\n" ${s} > $d/list_${h}_${c}.txt && ${SEQKIT} grep -f $d/list_${h}_${c}.txt $d/${ID}_${h}_hprc_r2.fa.gz -j 12 | ${BGZIP} -@12 > $d/${ID}_${h}_${c}-chm13.fna.gz
                                                ###generate centromere FASTA
                                                        ${DNABRNN} -Ai ${MODELS} -t32 $d/${ID}_${h}_${c}-chm13.fna.gz > $d/$h/cent/${ID}_${h}_${c}.bed && awk -F '\t' '$4==2' $d/$h/cent/${ID}_${h}_${c}.bed > $d/$h/cent/${ID}_${h}_${c}_cent.bed && ${BEDTOOLS} getfasta -fi $d/${ID}_${h}_${c}-chm13.fna.gz -bed $d/$h/cent/${ID}_${h}_${c}_cent.bed | ${BGZIP} -@12 > $d/$h/cent/${ID}_${h}_${c}_cent.fna.gz;
                                                done
                                        done
                                #some other operations here

```

Until now, I have never had issues but I’m afraid the `CONTIG` variable would get overwritten if not created with the specific `ID_HAP_CHR` combination. Sill, if this is not a problem I would like to know how to do it for the sake of improving my scripting. Thanks!

---

<div class="post-metadata">

**Author:** ![Skrynesaver](https://community.unix.com/user_avatar/community.unix.com/skrynesaver/32/14143_2.png) [@Skrynesaver](https://community.unix.com/u/Skrynesaver)\
**Post date:** [August 18, 2025, 9:19am UTC](https://community.unix.com/t/assign-environment-variable-to-a-variables-name/397233/2 "2025-08-18T09:19:51Z")

</div>

Hi Overcrtaft,

Welcome to the wonderful world of data structures 😉 where you get to design how your data is stored so as to enrich it with meaning.

When you need to have a variable name have a different value depending on the value of another variable, you can use an associative array to relate them.

eg.

```bash
#! /bin/bash

declare -A colour

fruits=("apple" "orange" "banana" "cherry")

colour["apple"]="green"
colour["orange"]="orange"
colour["banana"]="yellow"
colour["cherry"]="Red"

#for fruit in ${fruits[*]}; do
# Treat array members, protect them with quotes
for fruit in "${fruits[@]}"; do
        echo "$fruit is ${colour[$fruit]}"
done

```

---

<div class="post-metadata">

**Author:** ![overcraft](https://community.unix.com/letter_avatar/overcraft/32/6_449c533f586939b6e5f246632a46421f.png) [@overcraft](https://community.unix.com/u/overcraft)\
**Post date:** [August 18, 2025, 12:08pm UTC](https://community.unix.com/t/assign-environment-variable-to-a-variables-name/397233/3 "2025-08-18T12:08:39Z")

</div>

@Skrynesaver I see, that makes sense since your example is pretty straightforward! So, in my case should I do something like the following:

```bash
#! /bin/bash

declare -A contig

HAP=( "hap1" "hap2")
CHR_P=("chr1" "chr2" "chr3" "chr4" "chr5" "chr6" "chr7" "chr8" "chr9" "chr10" "chr11" "chr12" "chr13" "chr14" "chr15" "chr16" "chr17" "chr18" "chr19" "chr20" "chr21" "chr22" "chrY" )
CHR_M=("chr1" "chr2" "chr3" "chr4" "chr5" "chr6" "chr7" "chr8" "chr9" "chr10" "chr11" "chr12" "chr13" "chr14" "chr15" "chr16" "chr17" "chr18" "chr19" "chr20" "chr21" "chr22" "chrX" )

contig["hap1" "chr1"]="contig_hap1_chr1"

```

with all possible combinations of `HAP` and `CHR_P/M`? Also, these are arrays I created within my script by assignment; on the other hand, `ID` is obtained from a list of paths containing the same name folder as per the sample in question (_see_ below), how can I deal with it in the `declare` statement? Thanks again!

**names.list** first three entries

```bash
/path/to/hprc_y2_assemblies/HG00097_f
/path/to/hprc_y2_assemblies/HG00099_f
/path/to/hprc_y2_assemblies/HG00126_m

```

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_449c533f586939b6e5f246632a46421f.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [August 18, 2025, 6:22pm UTC](https://community.unix.com/t/assign-environment-variable-to-a-variables-name/397233/4 "2025-08-18T18:22:48Z")

</div>

In bash an **associate array** (or **hash** ) takes one string as index. Multi-dimensional indexes are not supported. So for clarity write

```bash
contig["hap1 chr1"]="contig_hap1_chr1"

```

where the space is a field separator that may not occur within the fields.  
In your script, within the nested loops you can construct it like

```bash
  contig["${h} ${c} ${ID}"]=$CONTIG

```

---

<div class="post-metadata">

**Author:** ![overcraft](https://community.unix.com/letter_avatar/overcraft/32/6_449c533f586939b6e5f246632a46421f.png) [@overcraft](https://community.unix.com/u/overcraft)\
**Post date:** [August 19, 2025, 10:54am UTC](https://community.unix.com/t/assign-environment-variable-to-a-variables-name/397233/5 "2025-08-19T10:54:34Z")

</div>

Hi @MadeInGermany got it,

I guess it will take me a bit of experimentation to have this done the right way, but I’ll try to integrate it in the script I have done. Thanks!
