Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 at 11:43

1 Answer 1

up vote 1 down vote accepted

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

cdpath=(${cdpath/(#s)$PWD(#e)})

(#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,

cdpath=(${cdpath/(#s)${$(some_command)}})

should work.

Test case:

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