This question already has answers here:
What is indirect expansion? What does ${!var*} mean?
(6 answers)
How to use a variable's value as another variable's name in bash [duplicate]
(6 answers)
Closed 5 years ago.
In the UNIX terminal, when I write the following commands:
$ a=b
$ c=a
$ echo $$c
I expected the output to be b, since the value of c is a and the value of a is b.
But , instead the output I received was: 2861c.
Can someone tell me the reason behind this output?
echo $$c prints your terminal PID and letter 'c' after it. You can verify it by 'ps aux | grep bash'.
From http://www.tldp.org/LDP/abs/html/internalvariables.html
$$ gives PID of the current running shell instance.
bash4$ echo $$ 11015
bash4$ echo $BASHPID 11015
The first $ sign captures the next character and prints the value.
For your case, a double substitution is the best option.
echo ${!c}
or you may go for
eval echo \$$c
$$ is special variable for BASH and used to print the process id of execution of current script. So $$c prints process id followed by c letter
If you still want to archive Indirect reference to variable,
a=b
c=a
echo ${!c} #will print "b" on console
Related
This question already has answers here:
Why doesn't ''var=value echo $var'' emit value? [duplicate]
(1 answer)
In Bash, why `x=100 echo $x` doesn't print anything? [duplicate]
(2 answers)
Closed 3 years ago.
I am currently trying to familiarise myself with shell line and command execution.
Could anybody explain to me the following behaviour? Why does shell only register an assignment variable when there is a separator before the command? Can assignment not be made in the same command?
sh-3.2$ x=5 echo ${x}
sh-3.2$ x=5; echo ${x}
5
sh-3.2$ x=5 && echo ${x}
5
When you don't have the separator, you're setting x as an environment variable while echo runs (only for the duration of that single command), without defining a shell variable for use in evaluating expansions before the command is started (which is what's happening in the x=5; echo "$x" case).
However, echo doesn't change its output based on which environment variables are defined (except sometimes very specific environment variables intended to modify its behavior -- POSIXLY_CORRECT and the like), so this has no visible effect.
This question already has answers here:
Use the result of a shell command in a conditional in a makefile
(1 answer)
How to use Bash parameter substitution in a Makefile?
(1 answer)
Closed 4 years ago.
I have next rule in Makefile:
dbrestoretable:
echo ${TABLE:-asdf}
When I run:
$ make dbrestoretable
echo
But:
$ echo ${TABLE:-asdf}
asdf
Why default value asdf is not echoed in first case?
UPD
Specified duplicate speaks about how to use variable assigned in Makefile. And it does not answer why default value is not used
Look at this modified example:
dbrestoretable:
echo ${TABLE:-asdf}
echo ${TABLE}
Then run:
$ make dbrestoretable TABLE=mytable
echo
echo mytable
$ make dbrestoretable
echo
echo
As you can see when I use ${TABLE:-asdf} it just return empty string. But I expect in first run mytable and second run asdf value to be echoed
This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 6 years ago.
I wanted to direct the output of rev command to a variable, I tried different methods and didn't work.
read -p "Enter the number: " n
echo $n | rev
echo "new n is: $p"
I want to assign the output of line 2 to p. How?
Thank you,
To store the output of a command in a variable use a $(...) command substitution:
p=$(echo $n | rev)
For further reference, you can check this link
This question already has answers here:
Why can't I specify an environment variable and echo it in the same command line?
(9 answers)
Closed 7 years ago.
In bash, I can pass an environment variable to a single command in the following way:
KEY=VAL <command>
However, I don't understand why the following doesn't work:
KEY=VAL echo $KEY
While this works:
KEY=VAL bash -c 'echo $KEY'
i.e. the first one prints a blank line while the other prints "VAL". I'd expect both to print "VAL".
Because KEY=VAL echo $KEY isn't having echo expand the $KEY variable.
The current shell is doing that before it runs echo (or whatever).
This question already has answers here:
grep for expression containing variable
(2 answers)
Closed 9 years ago.
In my bash script, I am attempting to parse through a status file and detect errors based on some keywords. I store these prefixes in a list, and then loop through them.
Bash script:
status_page="/path/to/file.txt"
list="aaa bbb ccc ddd"
for pre in $list
do
echo "grep '\w\w\w${pre}-.*\.lin failed' ${status_page}" # debug
if grep '\w\w\w${pre}-.*\.lin failed' ${status_page}; then
echo "Found error!"
exit 8;
fi
done
/path/to/file.txt:
xyzfff-tool.lin failed
xyzggg-exec.lin failed
rstccc-tool.lin failed
The bash script should catch the line rstccc-tool.lin failed, but it skips right over it.
For debugging, I print the grep commands verbatim, and when I copy that line and issue the command in my shell (tcsh), it returns that line...
Shell:
$ grep '\w\w\wccc-.*\.lin failed' /path/to/file.txt
rstccc-tool.lin failed
$ echo $?
0
If grep can find the line when I issue the command normally, how come it won't find it when the bash script is calling grep?
The variable won't be expanded in single quotes. Try with double quotes:
if grep "\w\w\w${pre}-.*\.lin failed" "${status_page}"; then
The ${pre} portion of that script is not parsing it correctly. I believe you need that line to say:
if grep '\w\w\w'${pre}'-.*\.lin failed' ${status_page}; then
... where the ${pre} is outside the quotation, such that bash will do the correct string replacement before sending it to grep.