1

I have a question to an answer I've read here. If I want to redirect the output of the time command with

{ time somecommand ; } &> file.txt

Why is the empty space in the beginning and the semicolon needed? And is there a difference if I just used

(time somecommand) &> file.txt
P.P
  • 117,907
  • 20
  • 175
  • 238
Picard
  • 983
  • 9
  • 23
  • Why don't you try it and find out? How are you curious enough to ask the question but not have tried to answer it?! – jonrsharpe Mar 19 '17 at 10:41
  • Thanks for this helpful comment...Of course I've tried it. But it doesn't work if I ommit the space and I'd like to learn more about it. I want to understand why this is and what's the difference between the different possibillities to use parantheses. And only because I couldn't notice a difference between the two commands, doesn't mean that there isn't one. – Picard Mar 19 '17 at 10:45
  • So *mention that*. Show your working; what have you tried, what have you read, ...? – jonrsharpe Mar 19 '17 at 10:46

1 Answers1

3

And is there a difference if I just used (time somecommand) &> file.txt ?

The difference is command(s) in ( ... ) run in a subshell;it's a different process. Whereas, commands under {...} runs in the same shell. From bash manual:

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

As for the spaces in compound commands, it's required by the bash grammar:

{ list; }
          list is simply executed in the current shell environment.
          list must be terminated with a newline or semicolon.  This is
          known as a group command.  The return status is the exit
          status of list.  Note that unlike the metacharacters ( and ),
          { and } are reserved words and must occur where a reserved
          word is permitted to be recognized.  Since they do not cause a
          word break, they must be separated from list by whitespace or
          another shell metacharacter.
P.P
  • 117,907
  • 20
  • 175
  • 238