Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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

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 '14 at 20:07
up vote 4 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.


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


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.