2

In this POSIX shell function of mine:

disable_mouse_for_a_second()
{
    if xinput --disable "$1" 2> /dev/null
    then
        (
            sleep 1s
            xinput --enable "$1"
        ) &
        return 0
    else
        return 1
    fi
}

Is ( ... ) a subshell in this code or something else?

Vlastimil Burián
  • 3,024
  • 2
  • 31
  • 52
  • Answer -- yes, it's a subshell that backgrounds `sleep` and `xinput`. It's just `( sleep 1s; xinput --enable "$1"; ) &` spread over multiple lines (which is 100% OK) – David C. Rankin Feb 13 '18 at 06:20

1 Answers1

3

The short answer to your question is Yes. The reason why is the block in question is nothing more than the single line:

( sleep 1s; xinput --enable "$1"; ) &

spread over multiple lines. It simply executes (and backgrounds) the sleep and xinput commands.

The two primary compound commands written over multiple lines you will see are:

(list)        ## and 
{ list; }

The distinction between the two is (list) is executed in a subshell environment and variable assignments and builtin commands that affect the shell's environment do not remain in effect after the command completes.

{ list; } list is simply executed in the current shell environment.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85