0

I need to setup an array and use that to loop through and list the files. I started with this setup and it works.

file = (x1 x2 x3 x4 x5)
for var in ${file[@]}; do
    echo $var
done

I have a requirement to add the timestamp on these variables, I tried a few ways and none of the seem to work. Here is one way I tried to define the array:

today=$(date +"%Y%m%d")
file = (x1_"$today" x2_"$today" x3_"$today" x4_"$today" x5_"$today")

What would be the best way to define the array, where the array element has a variable?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Choolai Raj
  • 111
  • 2
  • 8

1 Answers1

1

The way you're using "$today" is correct, no problem there.

Don't put spaces around the equal sign. It needs to be file=(...) in both cases. Spaces are not allowed in assignments.

You can use Shell Check to catch these kinds of errors. Here's what it prints:

file = (x1_"$today" x2_"$today" x3_"$today" x4_"$today" x5_"$today")
     ^-- SC2283: Remove spaces around = to assign (or use [ ] to compare, or quote '=' if literal).
       ^-- SC1036: '(' is invalid here. Did you forget to escape it?
       ^-- SC1088: Parsing stopped here. Invalid use of parentheses?
John Kugelman
  • 349,597
  • 67
  • 533
  • 578