I saw codes like this:
fqdn='computer1.daveeddy.com'
IFS=. read hostname domain tld <<< "$fqdn"
echo "$hostname is in $domain.$tld"
# => "computer1 is in daveeddy.com"
I think it works because IFS
is assigned to .
in the third line.. So I tried this:
x=100 echo $x
but found the bash doesn't print anything, while I expect it will print 100
..
Moreover, I found x=100 echo $x; echo $x
print nothing, while x=100; echo $x
prints 100
, which is very confusing.
Does anyone have ideas about this?
here is another way to think about it:
This is equal to:
Because at the time of scanning the line, $a is empty. Variable substition occurs first, so the $a just disappears.
Compare this to a very similar statment:
The
$x
is expanded beforeecho
runs, and the result is passed toecho
as an argument.echo
does not use the value ofx
in its environment.In the first example,
read
uses the value ofIFS
in its environment to split the string it receives via the here string.