Home > Blockchain >  how to write bash && & in one line
how to write bash && & in one line

Time:02-23

how to write this in one line ?

reboot -d 5 -f &  ;echo 666

then it tells me

-sh: syntax error: unexpected ";"

I use follow replace before to test to avoid reboot and easy to test

 echo 555 & ; echo 666
-sh: syntax error: unexpected "; 

I have try others

[root@EPC-M6Y2C ~]# `echo 555 & `&&  echo 666
-sh: 555: not found
[root@EPC-M6Y2C ~]# `echo 555 & ` ;  echo 666
-sh: 555: not found
666



CodePudding user response:

Both & and ; terminate lists. & causes the list to run in the background, ; in the foreground.

If you use both, the & terminates the list and the ; tries to terminate an empty list, which is not allowed.

Just drop the ;:

reboot -d 5 -f & echo 666

CodePudding user response:

You would get the same error if you just wrote an isolated

;

For the same reason, you can not write i.e. echo x;;echo y. If you want to write an empty statement (no-op), use : as a placeholder, i.e.

echo x; :; echo y 

works fine. In your case, this would be

reboot -d 5 -f & : ;echo 666

However, I would simply drop the no-op and write

reboot -d 5 -f & echo 666
  • Related