Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: ${var:1:1:=y}



On Feb 3,  3:12pm, Ray Andrews wrote:
}
} I'm wanting the naive expansion of that syntax to do this:
} 
}      variable=${var:1:1:=y}
} 
} ... but it doesn't work. Can something like that be done?

Jumping several messages ahead:

} assign to 'variable' either the value of the second character of 'var' 
} (just one character), and if that does not exist, assign 'y'

You've forgotten that ${var=val} has the side-effect of assigning to
"var" if it is not set.  I think what you mean is what Lawrence already
demonstrated (again several messages ahead):

    variable=${${var:1:1}:-y}

However, you can do this with subscript syntax:

    variable=${var[2]:=y}

which either assigns to $variable the second character of $var, or
assigns "y" to both $variable and to the second character of $var.

E.g.:

torch% var=a
torch% variable=${var[2]:=y}
torch% typeset -m var\*
variable=y
var=ay
torch% 

You can also do ${var[2]::=z} to forcibly assign "z" to the second
character of $var.

On Feb 3,  7:18pm, Lawrence Velazquez wrote:
} Subject: Re: ${var:1:1:=y}
}
} Test cases should focus on the questionable behavior by discarding
} irrelevant details. Your case boils down to this:
}
}     % echo -
} 
}     % /bin/echo -
}     -
}     %

Yes, the manual says (in the introduction to section 17 Shell Builtin
Commands and at the top of "man zshbuiltins"):

  All builtin commands other than precommand modifiers, even those that
  have no options, can be given the argument `--' to terminate option
  processing.  This indicates that the following words are non-option
  arguments, but is otherwise ignored.  This is useful in cases where
  arguments to the command may begin with `-'.  For historical reasons,
  most builtin commands also recognize a single `-' in a separate word
  for this purpose; note that this is less standard and use of `--' is
  recommended.

(Actually the doc might not say exactly that due to a Yodl formatting
error, but that's what it is supposed to say.  The part about a single
`-' in a separate word is correct and is what matters here.)



Messages sorted by: Reverse Date, Date, Thread, Author