2

I am writing a small script in shell and I have a constant in upper cases as you can see in code ('PRIX'). My problem is, I would like to print the value of the constant using the variable that will be equal to the string ('PRIX'). I won't explain you why as it would be useless, but that would cut my entire code in half and simplify everything.


readonly _PRIX_=7
.
.
.

function trier {

#here it starts
#function call, change 'option' value for "_PRIX_"
option=$(equivalence "$option")

#HERE The line I need to fix to print '7' 
echo $'$option'

#Print '7', What I wanna recreate knowing 'option' value=="_PRIX_"
echo $_PRIX_ 

sort -t$":" -k'$option' "$le_depot"

}
codeforester
  • 39,467
  • 16
  • 112
  • 140
  • `main.sh: line 7: equivalence: command not found` `sort: invalid number at field start: invalid count at start of ‘$option’`. Doesn't `readonly _PRIX_=7; echo "$_PRIX_";` work? – KamilCuk Oct 13 '18 at 05:13
  • What is `option` set to? Your requirement is not clear. – codeforester Oct 13 '18 at 05:14
  • 'option' is a variable and it will be equal to the string '_PRIX_' but '_PRIX_' is also a constant and I wanna print '_PRIX_' value wich is '7' by using the variable calde 'option' – MattSmilenot Oct 13 '18 at 14:24

1 Answers1

0

If you are using Bash, indirection might help you. For example:

readonly _PRIX=7
option=PRIX
option_var=_$option
echo "${!option_var}" # outputs 7

Related:

codeforester
  • 39,467
  • 16
  • 112
  • 140