-2

enter image description hereI have a problem with head command in linux when I assign a variable to it like a=date and then echo $a I do get the result ! but when i use a= he ad date and want to echo result it just go to A new line without any warning !why is it? how can I assign 2 command to be executed and stored in variable? like

b= `banner date`
echo $b
Hoda Fakharzadeh
  • 697
  • 3
  • 7
  • 18
  • @hek2mgl I think the second link was also relevant as it explains why `b=head date` works but `b= \`banner date\`` doesn't. – PesaThe Jan 20 '18 at 13:02
  • I see. Sorry if I wasn't careful enough. I think you can re-add it to the list. – hek2mgl Jan 20 '18 at 13:16
  • 1
    @hek2mgl I am sorry, I don't know how to do that :) I will just post it here. I recommend checking out [Command not found error in Bash variable assignment](https://stackoverflow.com/questions/2268104/command-not-found-error-in-bash-variable-assignment). – PesaThe Jan 20 '18 at 15:16
  • why does it goes to a new line after I type echo $b (in the picture)? – Hoda Fakharzadeh Jan 21 '18 at 07:51
  • @HodaFakharzadeh refer to the link in the comment above. `b=head date` runs `date` with `b` set to **string** `head` in its environment. It is actually not a variable assignment so `$b` is still empty. Therefore when you `echo` an empty variable, it prints nothing + newline (`echo` appends the newline). – PesaThe Jan 22 '18 at 01:46

1 Answers1

1

If I understood the question correctly,

"You can assign 2 command to be executed and stored in variable" as below -

$ a=$(date ; uptime)

Output -

$ echo $a
Sat Jan 20 18:17:54 IST 2018 18:17 up 1 day, 6:14, 9 users, load averages: 1.99 1.85 1.74
vivekyad4v
  • 13,321
  • 4
  • 55
  • 63