0

I have an input file which has data as shown below:

name,local_unit,region_unit
AAAAA,hour,cell
BBBBB,15min,cell

Here is my shell

#!/bin/bash

file_path='/home/vikrant_singh_rana/test_bq_file.csv'

{ read while IFS=, read -r name local_unit region_unit do echo $name echo $local_unit echo $region_unit

    ingest_tablename=ABC_XYZ_$name
    echo $ingest_tablename
    aggr_tablename=ABC_XYZ_$name_$local_unit_$region_unit_aggregation
    echo $aggr_tablename

done

} < $file_path

It is able to populate the table name for variable ingest_tablename and not for aggr_tablename

Output as shown below:

AAAAA
hour
cell
ABC_XYZ_AAAAA
ABC_XYZ_
BBBBB
15min
cell
ABC_XYZ_BBBB
ABC_XYZ_
thanasisp
  • 8,122

1 Answers1

4

Because the underline _ is a valid character in variable names, ABC_XYZ_$name_$local_unit_$region_unit_aggregation tries to expand variables called $name_, $local_unit_ and $region_unit_aggregation. The first two are probably wrong. To stop that, put the variable names in question in braces, i.e.

aggr_tablename="ABC_XYZ_${name}_${local_unit}_$region_unit_aggregation"

(or aggr_tablename="ABC_XYZ_${name}_${local_unit}_${region_unit}_aggregation" if that's what it should be.)

The quotes aren't strictly necessary in the assignment but otherwise a good idea.

FWIW, shellcheck.net tells of the problem, but sadly not how to solve it, it says:

^-- SC2154: name_ is referenced but not assigned (did you mean 'name'?).

ilkkachu
  • 138,973