0

If you write a 1 line shell script that forks a process, like the following: for i in {1..1000};do ./binary & done

It runs, you don't need a semicolon, and if you try to use a semicolon it throws a syntax error.

In a normal loop, without forking, you need the semicolon for it to work in a 1 line script. Why does the fork operator change how this works?

Cyrus
  • 12,059
  • 3
  • 29
  • 53
john doe
  • 746
  • 1
  • 12
  • 27
  • Related: [Use & (ampersand) in single line bash loop](https://unix.stackexchange.com/questions/91684/use-ampersand-in-single-line-bash-loop) – steeldriver Mar 06 '21 at 03:29
  • 2
    You end a command with either `;` or `&` depending on if you want the pipeline to run in the foreground or the background. A newline implicitly adds a semi-colon if the command can logically finish at that point. – icarus Mar 06 '21 at 04:07
  • 1
    The last sentence in the question body ends with `?`. Usually we use `.` to end a sentence. Why does the question mark change how this works? Why not `?.`? The same situation. You call `&` "fork operator" but it's a separator/terminator. So is `;`. You use one xor the other. – Kamil Maciorowski Mar 06 '21 at 05:47
  • Possible duplicate of [How can one run multiple programs in the background with single command?](//unix.stackexchange.com/q/67006) – Stéphane Chazelas Mar 06 '21 at 06:11

0 Answers0