Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

zsh includes a powerful utility for parsing command-line options zparseopts, but for quick shell scripts and shell functions, but is there an easy way to extract the array of all command-line arguments that do not begin with a hyphen?

share|improve this question
    
ok, searching online i came across something like nohyph=(${argv##-*}). I'm not sure how robust it is... –  kjo Feb 8 at 20:07

1 Answer 1

up vote 2 down vote accepted

Filter the positional parameters $@ with the parameter expansion suffix :#-* to strip elements matching the pattern -* and the parameter expansion flag @ inside double quotes to preserve empty elements. Add the M flag to retain only the elements that match the pattern.

non_hyphen_arguments=("${(@)@:#-*}")
hyphen_arguments=("${(@M)@:#-*}")

However this is not a good way of parsing command line arguments; for example, given

myscript hello -a world

you will get hello and world in non_hyphen_arguments and -a in hyphen_arguments.

The simpler form of argument parsing, with single-letter options, is getopts.

share|improve this answer

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.