1

I'm at my wits end with this. I'm using zsh and trying to simply split the expansion of %3~ on the / character. This works fine with the expansion of $(pwd) and $PWD - both of which are also path-like strings.

I'm trying to split a string using the following code ("${(@s./.)a}") - where a is either a=$(pwd) or a=%3~. Again this code works with pwd, but not %3~.

I'm still a bit new to shell scripting -- but as far as I understand it, there should be no discrepancy. No matter what I do, I cannot get the expansion of %~ to split properly. Things I've tried:

# Ran from /home/me/code
a=%3~
b=$(pwd)
c=$PWD
d="/test/1/2"

e=$(echo "$a")
f="/$a"

echo $a # ~/code
echo $b # /home/me/code
echo $c # /home/me/code
echo $d # /test/1/2
echo $e # ~/code
echo $f # /~/code

("${(@s./.)a}") # ~/code
("${(@s./.)b}") # home me code
("${(@s./.)c}") # home me code
("${(@s./.)d}") # test 1 2
("${(@s./.)e}") # ~/code
("${(@s./.)f}") # /~/code

I'm actually starting to think that zsh is just intentionally inconsistent. I'm sure I'm just missing something, though. I've also tried ("${(ps./.)a}"), because as I read it that should do the same. I've also tried IFS-based splitting, setting IFS="/". Again, works for normal strings - does not work for %~. ???

EDIT: For the sake of completeness, this is within a .zsh-theme file where. I suppose maybe this matters? I'm technically not the one executing it (it's being ran by oh-my-zsh), so maybe something weird is happening at that level.

2
  • echo $a should by default output %3~, not ~/code.
    – Kusalananda
    Apr 5, 2020 at 10:07
  • @Kusalananda - Oddly not - which is part of the reason this is so confusing to me. It outputs ~/code and I can echo it just like any other string. Only splitting (and I'm sure other things I haven't tried) end up failing.
    – 1f928
    Apr 5, 2020 at 18:17

1 Answer 1

2

You are attempting to use an prompt escape outside of the context where prompt escapes are normally evaluated. so the variable contains the string %3~, you attempt to split the string %3~ resulting in %3~ and i am assuming it is later assigned to PS1/PROMPT which zsh then expands %3~.

If you would like to perform parameter expansions and have the string %3~ treated like a prompt escape you can use the parameter expansion flag % like so ${(%)a}

2
  • thanks! It worked. For anyone interested, here's a good resource for understanding parameter expansion a little better. I'm still unclear as to why prompt parameter expansion had to be used when I could just echo parameter and get the value. Oh well - ("${(@s./.)${(%)b}}"), where b=%3~ works.
    – 1f928
    Apr 5, 2020 at 18:29
  • because you don't get the value by "just by using echo" it is a string that the shells treats special when drawing your prompt.
    – llua
    Apr 5, 2020 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.