Why parameter expansion does not work in Makefile? [duplicate] - bash

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

Related

Operator in variable affectation [duplicate]

This question already has answers here:
What is the difference between "var=${var:-word}" and "var=${var:=word}"?
(4 answers)
Closed 1 year ago.
I'm trying to understand the syntax of an existing ksh script. I came across the following line:
HOME_APP=${HOME_APP:-/app}
What does it mean?
Seems like there must be a duplicate for this, but :- is used to supply a default value for the expansion when HOME_APP is unset or null.
$ unset HOME_APP
$ echo "${HOME_APP:-/app}"
/app
$ HOME_APP=
$ echo "${HOME_APP:-/app}"
/app
$ HOME_APP=/opt
$ echo "${HOME_APP:-/app}"
/opt

Get a variable declared into eval command [duplicate]

This question already has answers here:
How can I reference a file for variables using Bash?
(9 answers)
Closed 2 years ago.
I want to use a variable initialized into a subscript, in order to avoid returning it with echo or return.
For example, my goal is to output $myvar with this script:
eval "./script.sh"
echo -n $myvar
declared inside script.sh like this:
declare -gx myvar # global and exported var
myvar=42
Output:
nothing
Is there another command or flag to add in replacement to declare -gx ? Or moving it before eval is the solution ?
You can use source instead of eval:
source "./script.sh"

Why doesn't "x=5 echo ${x}" output anything? [duplicate]

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.

Weird output in BASH shell [duplicate]

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

Why environment variables aren't being passed in a single command [duplicate]

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).

Resources