3

I have this line in a function I'm adapting from a bash function into zsh function:

local info=( $(command df -P $fs | awk 'END{ print $2,$3,$5 }') )

Whenever I call it in the func I get this:

mydf:9: bad pattern: info=( 712687280

But when I call it from the shell I get the expected answer:

$ info=($(command df -P $fs | awk 'END{ print $2,$3,$5 }'))
$ echo $info                                                                                      
712687280 166242288 25%

I am 2 days into zsh so I'm a little ignorant as to what's causing the problem.

7

Put the assignment on a separate line from the local definition:

local info
info=( $(command df -P $fs | awk 'END{ print $2,$3,$5 }') )
  • 1
    Could you please elaborate why the separate line is needed? I see it is working on zsh 5.1.1 (Cygwin), but fails on 5.0.2 (Linux). – Alexander Groß Dec 21 '15 at 0:36
  • 1
    @AlexanderGroß when declaring and setting a local variable in a single command, apparently the order of operations is to first set the variable, and only afterwards restrict it to local scope. Try this: – Chauncey Garrett Jan 29 '16 at 19:33
  • local unexpected=$(exit 1) && echo $? # 0 – Chauncey Garrett Jan 29 '16 at 19:37
  • local expected && expected=$(exit 1) && echo $? # 1 – Chauncey Garrett Jan 29 '16 at 19:38

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.