Why do I have different results when using commands with pipelines as opposed to && concatenated commands in bash?

I implemented a small python function for current_datetime and pasted it into a bash script.

$ current_datetime

2017-08-29 12:01:18.413240

I later assigned it to a variable

$ DT=$(current_datetime)

What can i call

$ echo $DT

2017-08-29 12:03:48.213455   #and get a time some seconds later for sure

But if I run the next line several times, I get the same results (note the same decimal part of the seconds in bold )

$ DT=$(current_datetime) | echo $DT

2017-08-29 12:04:42.**544683**


$ DT=$(current_datetime) | echo $DT

2017-08-29 12:04:42.**544683**


$ DT=$(current_datetime) | echo $DT

2017-08-29 12:04:42.**544683**

In turn, when I use &&instead |, I got the exact time when the button was pressed Enter. Why?

$ DT=$(current_datetime) && echo $DT

2017-08-29 12:21:**11.564654**


$ DT=$(current_datetime) && echo $DT

2017-08-29 12:21:**13.522406**


$ DT=$(current_datetime) && echo $DT

2017-08-29 12:21:**14.744963**

What is the difference between |and &&regarding its implementation on the same command line and the exact moment of their execution?

+4
1

pipeline . , .

$ FOO=bar | echo $FOO

$ echo $FOO

$

list, &&, , .

$ FOO=bar && echo $FOO
bar
$

.

$ (FOO=bar) && echo $FOO

$
+4

Source: https://habr.com/ru/post/1684684/


All Articles