Combine two shell commands into one output in a shell

I am trying to combine several teams into one output.

#!/bin/bash x=$(date +%Y) x=$($x date +m%) echo "$x" 

It returns

./test.sh: line 4: 2011: command not found

+4
source share
6 answers
 x=$(echo $(date +%Y) $(date +%m)) 

(Note that you have migrated the % and m characters to the month format.)

+6
source

In the second line, you are trying to execute $x date +m% . At this point, $ x will be installed in the year 2011. Now he is trying to run this command:

 2011 date +%m 

This is not what you want.

You can do it:

 x=$(date +%Y) y=$(date +%m) echo "$x $y" 

Or what:

 x=$(date +%Y) x="$x $(date +%m)" echo "$x" 

Or just use the final date format right away:

 x=$(date "+%Y %m") echo $x 
+5
source

Maybe this?

 #!/bin/bash x=$(date +%Y) x="$x$(date +%m)" echo "$x" 

... also corrects what appears to be a transposition in the format string that you passed to date a second time.

+1
source

A semicolon to separate commands on the command line.

 date +%Y ; date +m% 

Or if you want to run only the second command, if the first is successful, use a double ampersand:

 date +%Y && date +%m 

Or, if you want to run both commands at the same time, unpredictably mixing their output (maybe not what you want, but I thought I would be thorough), use one ampersand:

 date +%Y & date +%m 
0
source
 echo Β΄date +%YΒ΄ Β΄date +m%Β΄ 

Note the reverse accent (')

0
source
 echo `date +%Y` `date +%m` 

And to make a minimal change to the OP:

 x=$(date +%Y) x="$x $(date +%m)" echo "$x" 
0
source

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


All Articles