When you run "./a.sh $t
your current shell evaluates $t
to '' so $1
is unset in your script and it will just execute echo
.
If you quote the the variable either ./a.sh \$t
or ./a.sh '$t'
your script will do echo '$t'
. You can then use either eval to get it to evaluate the expression:
eval echo "$1"
or preferable strip off the leading '$' and use indirect variable:
var=${1:1}
echo "${!var}"
If you just need to store data use json or sqlite instead of a script.
If you have logic in your script consider just passing in the variable names and if not set dump all variables (using the name convention that your variables are lower case):
#!/bin/bash
if [ -z "$1" ]
then
set | grep "^[a-z]"
exit 0
fi
for v in "$@"
do
set | grep "^$v="
done
and you then do:
$ ./a t
t=1232
$ ./a
t=1232