Search code examples
macosvariableszshiterm

zsh variable assignment within if


This is my first question here so ... i hope i do it right.

I'm trying to write a little zsh script - following example simplifies what I have:

function example {
    if [ $1 == "my_string" ]; then
        local my_variable=10
    fi
    echo $my_variable
}

// i want
$> example my_string
$> 10

// i get
$> example:1: = not found

I've been trying with and without local variables, i tried declare and with or without spaces aswell as googling for hours.

t.i.a.

EDIT: ADDITIONAL INFORMATION I'm using Mac OSX Mavericks - iTerm2 and I installed oh-my-zsh - this script is written directly within my ~/.zshrc


Solution

  • In zsh, when the equals option is set, typing =program will expand to the path to that program. Here, zsh is interpreting == as "path to the = program" and, not finding it, printing an error. You can either quote it or use [[ ... ]] instead.