Search code examples
shposixsubshell

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


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?


Solution

  • 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.