0

cat a.sh

#!/bin/bash
t=1232
echo $1
#echo $t

when i run the script "./a.sh $t" I can't get value 1232 when the script replacement with echo $t ,run the script "./a.sh" can get vaule 1232

can anybody else tell me ,if i use "./a.sh $t" this form ,how can get the vaule,thanks alot

have no ideas to get the variables throug the termi

hisktskr
  • 3
  • 2
  • 2
    The script consists only of this single line???? Please [format your code properly](https://stackoverflow.com/help/formatting) – user1934428 Jan 13 '23 at 07:17
  • 1
    I'm not sure exactly what you're trying to do. Does ["How do I set a variable to the output of a command in Bash?"](https://stackoverflow.com/questions/4651437/how-do-i-set-a-variable-to-the-output-of-a-command-in-bash) answer your question? If not, please clarify what your goal is. – Gordon Davisson Jan 13 '23 at 08:11
  • `$t` gets evaluated immediately, the script you are running simply receives whatever it evaluated to in `$1` (probably nothing). I'm guessing maybe you are looking for [indirect references](https://stackoverflow.com/questions/16553089/dynamic-variable-names-in-bash) – tripleee Jan 15 '23 at 08:44

2 Answers2

0

I think your variable t are out of scope. Therefore, what you want to do is assign the variable t=1232, beforehand, and use it as an argument. So the script would be

#!/bin/bash
echo $1

Then call the script as you wanted to with variable t already assigned to the value, so it would print the desired output

t=1232
./script.sh $t

I think that's that. would love to hear your thoughts tho

0

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
Allan Wind
  • 23,068
  • 5
  • 28
  • 38
  • Actually, without quotes, it simply leaves `$1` unset if the variable `t` was empty or unset in the calling shell. Perhaps also see [When to wrap quotes around a shell variable?](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – tripleee Jan 15 '23 at 08:41
  • @tripleee I don't think that I am anything different. `$1` is unset because `$t` is being evaluated by calling shell. Clarified it, hope it addresses your concern. – Allan Wind Jan 15 '23 at 08:48
  • I'm afraid it's actually still incorrect; you are running `echo`, not `echo ''`. Probably add quoting to your solution, though with `eval`, I can't really say what the correct quoting would look like. – tripleee Jan 15 '23 at 08:53
  • @tripleee Neither seems wrong to me but not really sure. The code is `echo $1` and if a variable is not set it evaluates to '', right? $(echo) = $(echo ''). Do you have a reference for why `echo ''` is "wrong"? I revised answer assuming that I am wrong, btw, but still curios. – Allan Wind Jan 15 '23 at 08:56
  • 1
    `echo` is a bad example as such because it prints an empty line in both cases. There are many other commands where an empty argument does something different than no argument; try e.g. `cat ''` vs `cat`, for example. So it does matter to keep them separate and distinct, especially when trying to explain what actually is going on here. – tripleee Jan 15 '23 at 09:00