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

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.

Related

using command line arguments in loop shell [duplicate]

This question already has answers here:
Brace expansion with a Bash variable - {0..$foo}
(5 answers)
Closed 3 years ago.
I am trying to use command line arguments for arithmetic but cant seem to find any documentation explaining how to do this. As an example if I use:
for i in {$1..$2}
do
echo $i
done
and call
test.sh 1 20
the following output is produced:
{1..20}
instead of
1
2
3
..
20
The following will also work:
declare -a ary='({'$1..$2'})'
for i in "${ary[#]}"; do
echo "$i"
done
Note that declare is as harmful as eval.
You need to check and sanitize the arguments before use.
There's no way to do this properly without the evil eval() with brace expansion in bash.
You can use seq instead :
for i in $(seq $1 $2); do

How to receive command line argument in shell script for loop [duplicate]

This question already has answers here:
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed 3 years ago.
How to receive command line argument in shell script for loop? e.g
vim batch_echo
for i in {1..$1}; do echo $i; done
sh batch_echo 3
{1..3}
but if change to seq, it's ok
for i in `seq 1 $1`; do echo $i; done
sh batch_echo 3
1
2
3
so why {1..$1} cannot work?
Just like bash, I think it's because brace expansion occurs before expansion of variables.
I recommend you to go check: http://www.gnu.org/software/bash/manual/bashref.html#Brace-Expansion . Let me know if that helped or not.
Hope I could help.

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

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

Three asterisks in echo output inserting listing? [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 6 years ago.
Can anyone tell me why the following:
#!/bin/bash
TEST=$(echo '*** this is a test ***')
echo $TEST
...outputs a listing of the current directory, then "this is a test", then another listing of the current directory?
Background: I have some output that I'm putting in a variable, and then I want to do several different greps on the contents of that variable, but the echo is inserting all this extra stuff that shouldn't be there.
This is on OS X 10.11.4.
globbing in action! * expands in the second echo. You have to double quote it to prevent expansion.
echo "$TEST"
see this for a related answer

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