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 use zsh.

Say, for instance, that I want to remove the current directory from cdpath. That means that I need to remove an element based on content, which fits the ${name:#to_be_removed} pattern nicely. But there's a catch: to_be_removed is the result of substitution (namely, $(pwd)/). How would I pull this off in a elegant fasion? The best I have managed is this:

cdpath=("${(s/ /)$(eval echo $(echo "\${(@)cdpath:#${$(pwd)}/}"))}")

This is a common use-case for me, and having tons of this *SH mess is starting to get on my nerves...

Thanks, PythonNut

share|improve this question
If you are tired of typing this ,,common use-case'', why don't you define a function for this? – mpy Jan 22 '14 at 11:43
up vote 1 down vote accepted

If you want to remove the current directory from the $cdpath array, I would simply use


(#s) and (#e) serve the same purpose as ^ and $ in ,,normal'' regular expressions, i.e. start and end of string. Please note: These will need setopt EXTENDED_GLOB to work!

If you need an external command,


should work.

Test case:

$ cdpath=(/foo /foo/bar /bar)
$ pwd
$ cdpath=(${cdpath/(#s)${$(builtin pwd)}})
$ print -l $cdpath
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.