0

I have a script with around 12 variables that do multiple checks on a user that is passed to my script with $1. What I would like to do is to be able to call a variable from the command line with $2. So for example I have variables in my script like so:

!#bin/bash

var1=blah0
var2=blah1
var3=blah2
var4=blah3

What I want is to be able to run the script like $ ./script.sh user var3 and that should return the value that the variable holds. Without doing a bunch of if then statements, how could I do this efficiently? I've tried arrays but I couldn't figure out how to pass a variable inside the array.

Michael
  • 37
  • 1
  • 7
  • See also [BashFAQ #6](http://mywiki.wooledge.org/BashFAQ/006). – Charles Duffy Apr 21 '20 at 16:30
  • you do not need if conditions , you can use bash indirect referencing. in your script you can add an echo statement like this and print the value of input variable | echo "value of $2 , ${!2}" – prudviraj Apr 21 '20 at 16:33

0 Answers0