Sample Header Ad - 728x90

Result of a command is not assigned to a variable

1 vote
1 answer
1103 views
I am pretty unexperienced (to put it mildly) when it comes to BASH and Shell scripting, so bear with me: $ toda=$(date) | echo $toda gives me: Fr 29 Mai 2020 15:25:19 CEST. So far so good. datediff is part of the dateutils package and gives back the number of days between to dates: datediff 2019-12-31 2020-05-29 gives me: 150. Again, so far so good. But: toda=$(datediff 2019-12-31 2020-05-29) | echo $toda gives me back: Fr 29 Mai 2020 15:25:19 CEST and not (as expected) 150. In other words it did not assign the datediff result but kept the value from the former operation unchanged. Of course I tried: anothervarname=$(datediff 2019-12-31 2020-05-29) | echo $anothervarname which gives back an empty variable (ie a blank line above the prompt). What do I have to do to assign the datediff result from the above example to a variable? Thanks for your help.
Asked by Guebert (13 rep)
May 29, 2020, 01:50 PM
Last activity: May 29, 2020, 01:53 PM