[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: bash и область видимости переменной в while



On 2014-02-18, Vladimir Skubriev wrote:

> Что делают внутренние скобки ?

Круглые - создают отдельный процесс. Фигурные - в текущем.

Из POSIX:

  Grouping Commands

  The format for grouping commands is as follows: 

  (compound-list) 

  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. 

  { compound-list;} 

  Execute compound-list in the current process environment. The semicolon
  shown here is an example of a control operator delimiting the } reserved
  word. Other delimiters are possible, as shown in Shell Grammar ; a <newline>
  is frequently used. 

Зачем может понадобиться { }? Из POSIX (см. пример XXX ниже):

  Each redirection shall apply to all the commands within the compound command that do not explicitly override that redirection. 


  $ ( var=1; ); echo x $var x
  x  x

  $ { var=1; }; echo x $var x
  x 1 x

  $ { var=x; echo $var; } | cat && echo $var
  x
  1
  $ { var=x; echo $var; } | cat && echo $var
  x
  1
  $ { var=x; echo $var; } >/dev/null && echo $var    # XXX
  x
  
  $ { false; true; } && echo ok || echo fail
  ok
  $ { true; false; } && echo ok || echo fail
  fail

  $ ( exit 0 ) && echo ok || echo fail                                                                                                                                                                                                                           
  ok
  $ ( exit 1 ) && echo ok || echo fail
  fail

  $ { echo user XXX YYY; echo cd /pub; echo put backup.tgz; echo exit; } | ftp -n server.org

-- 
Best regards!


Reply to: