0

I'm trying to set each element from for loop to var in Bash.

This is my line:

counter=1 ;
for i in $(ls /);
do
    echo line number $counter - $i  ;
    line"$counter"="$i" ;
    echo $(line$counter);
    counter=$[counter+1];
 done

What I am trying to do, is set each i variable to counted vars ($counter).

jww
  • 97,681
  • 90
  • 411
  • 885
  • 2
    How about using an array instead – that other guy Dec 20 '19 at 21:02
  • 1
    All you want is `var=(/*)`. – chepner Dec 20 '19 at 21:08
  • 1
    [Shellcheck](https://www.shellcheck.net/) reports several problems with the example code. See [Creating a string variable name from the value of another string](https://stackoverflow.com/questions/13716607/creating-a-string-variable-name-from-the-value-of-another-string) for ways to set variables whose names are created on the fly, but you probably should be using an array instead. – pjh Dec 20 '19 at 21:56
  • Line breaks were added for readability. Please revert the edit if you disagree. – jww Dec 21 '19 at 00:46
  • 2
    Possible duplicate: [Bash for loop to set a variable, its value, and evaluate it?](https://stackoverflow.com/questions/42047532/bash-for-loop-to-set-a-variable-its-value-and-evaluate-it) (Summary: use an array.) Also, [don't parse `ls`](https://mywiki.wooledge.org/ParsingLs). – Gordon Davisson Dec 21 '19 at 02:22

0 Answers0