[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT: bash positional parameters



On 2005-06-16, michael wrote:
> Folks
>
> For some reason out of my control I need to pass the name of an env var
> to a bash script and then, within said script, determine the value of
> that env var.
>
> For example if my script is 'myScript' and assume I've done
>
> 	$ export INPUTFILE=/tmp/whateverandaday
>
> then how inside myScript do I get the value of INPUTFILE when all I can
> pass to the script is the name of the env var, eg
>
> 	$ ./myScript INPUTFILE
>
> (and no I cannot pass $INPUTFILE directly for another reason [a later
> part of the script calls a program which needs the env var name not its
> value])
>
> I've tried various combos inside the script like 
>   echo ${$1}
> but they don't work (the nearest I can get is
>   set | grep $1
> but that's ugly IMHP)

   In bash you can do:

var=$1
INFILE=${!var}

   To make it portable, so that your script can be run in any
   Bourne-type shell:

eval "INFILE=\${$1}"

-- 
    Chris F.A. Johnson                     <http://cfaj.freeshell.org>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/cfaj/ssr.html>



Reply to: