70

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?

1

2 Answers 2

72

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.

4
  • In bash and other shells...?
    – jasonwryan
    Dec 5, 2011 at 0:20
  • 3
    bash is the one i was asking about... Dec 5, 2011 at 0:24
  • 14
    What's the difference between $() and ()? Dec 7, 2015 at 7:47
  • 11
    @CMCDragonkai The $() is command substitution, the () is a subshell. Both of them run commands, the difference is what happens to the output. The names are much easier to search than the symbols. See also unix.stackexchange.com/q/213530/9537
    – jw013
    Dec 7, 2015 at 18:19
21

A command list embedded between parentheses runs as a subshell.

Variables in a subshell are not visible outside the block of code in the subshell. They are not accessible to the parent process, to the shell that launched the subshell. These are, in effect, local variables.

See Linuxtopia - Chapter 20. Subshells

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .