-1

[[ -n $program ]] && echo "$program" && setsid $program "$arg" & && exit 0
gives me "syntax error near unexpected token `&&'"

[[ -n $program ]] && echo "$program" && { setsid $program "$arg" & } && exit 0
works fine

can someone help me understand this is there any difference in using brackts there

muru
  • 69,900
  • 13
  • 192
  • 292
Yogesh
  • 1
  • 1
    Does this answer your question? [How can I test if a background job failed before it exits?](https://unix.stackexchange.com/questions/454337/how-can-i-test-if-a-background-job-failed-before-it-exits) or [Bash: How to run a script in background with "&" and use "||" to run something else on failures?](https://unix.stackexchange.com/a/432434/70524) – muru May 31 '23 at 10:46
  • 6
    You can't get the exit status of a backgrounded program until it exits, so what's the `&&` supposed to be doing there? And the exit status of `setsid $program "$arg" &` is always going to be true, so what is the `exit 0` supposed to be conditional on? – muru May 31 '23 at 10:50

1 Answers1

2

Quoting the Bash manpage:

A list is a sequence of one or more pipelines separated by one of the operators ;, &, &&, or ||, and optionally terminated by one of ;, &, or <newline>.

Therefore I assume that its parser will see & && as a syntax error.

Grobu
  • 161
  • 5
  • 1
    This doesn't really answer the "_why?_" of the question – roaima Jul 08 '23 at 07:01
  • 2
    "why does using & before && gives me an error in bash script?" : because you can't separate a list with more than one control operator. It's an error on the syntactical level. I don't see how this does'nt answer the "why" of the question. – Grobu Jul 08 '23 at 07:31
  • Yes - `& &&` is inconsistent with the grammar that Bash accepts. Hence it's a syntax error. Perfectly clear. – Toby Speight Jul 08 '23 at 09:06