How Are Parentheses Interpreted in Command Line?

bashquotingshell

While reading up on how to set up grub, I came across an article claiming that I need to use one of the following two syntaxes,

echo \(hd0,0\) >> /boot/grub/grub.conf

or

echo '(hd0,0)' >> /boot/grub/grub.conf

because, at the command line, parentheses are interpreted in a special way. What is special about the parentheses? How are they interpreted?

Best Answer

Parentheses denote a subshell in bash. To quote the man bash page:

(list)    list  is  executed  in  a  subshell  environment (see COMMAND
          EXECUTION ENVIRONMENT below).  Variable assignments and builtin 
          commands that affect the shell's environment do not remain in 
          effect after the command completes.  The return status is the
          exit status of list.

where a list is just a normal sequence of commands.

This is actually quite portable and not specific to just bash though. The POSIX Shell Command Language spec has the following description for the (compound-list) syntax:

Execute compound-list in a subshell environment; see Shell Execution Environment. Variable assignments and built-in commands that affect the environment shall not remain in effect after the list finishes.