0

I am trying to accomplish something like this :

index=([0]=INDEX1 INDEX2 INDEX3 )

echo INDEX1 will print 0

echo INDEX2 will print 1

echo INDEX3 will print 2

is it possible to accomplish something like this ? or , like this :

echo ${!index[INDEX1]}    # print 0 
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • I don't understand the question. Do you know the `value` and want to know the `index` it has in the array? – mofoe Sep 03 '15 at 09:18
  • yes exactly , i want to use it like an enum or something in that sort.. – Pavel Krivosheev Sep 03 '15 at 09:48
  • note that this is not direct, since many indexes can have the same value. So the best you can probably do is to loop through all the values and return the index when it matches what you are looking for. – fedorqui Sep 03 '15 at 10:50

1 Answers1

1

bash doesn't really support using arrays like this. Besides, this wouldn't really be any simpler than

INDEX1=0
INDEX2=1
INDEX3=2

echo $INDEX1
chepner
  • 497,756
  • 71
  • 530
  • 681