Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 6 down vote accepted

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

local info
info=( $(command df -P $fs | awk 'END{ print $2,$3,$5 }') )
share|improve this answer
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 at 19:33
    
local unexpected=$(exit 1) && echo $? # 0 – Chauncey Garrett Jan 29 at 19:37
    
local expected && expected=$(exit 1) && echo $? # 1 – Chauncey Garrett Jan 29 at 19:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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