0

So, I have Oh My Zsh up and running, and I'm creating my own new zsh-theme. In it, I wish to grab the external IP address from https://api.myip.com - and I'm using curl & grep to grab it. Works fine when I enter it at the command prompt, but when embedded in my zsh-theme file it gives me an error:

zsh: no matches found: ((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5]).){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])
(23) Failed writing body

Jacobs-MacBook-Pro-2.local jacobjackson ttys002 0 [                               ] 10/29/20 18:32:46 PM

Here is my zsh-theme:

PROMPT='%F{white}%M %n %y %j $(curl -s https://api.myip.com | grep -oE '((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])') %F{green}%2c%F{blue} [%f '
RPROMPT='$(git_prompt_info) %F{blue}] %F{green}%W %* %F{yellow}%D{%p}%f'

ZSH_THEME_GIT_PROMPT_PREFIX="%F{yellow}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%f"
ZSH_THEME_GIT_PROMPT_DIRTY=" %F{red}*%f"
ZSH_THEME_GIT_PROMPT_CLEAN=""

And here is the command sequence that grabs the IP address:

curl -s https://api.myip.com | grep -oE '((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])'

1 Answer 1

0

First, $() commands don't run inside of single-quotes. Changing your exterior quotes to "" should fix this. This also fixes the issue where the interior quotes were pre-maturely closing the string (as you can see from the syntax highlighting).

Second, the interior part seems to be broken on my computer since you're using a fixed regular expression. The server returns an IPv6 IP address for me, but that regular expression is looking for an IPv4 one. You can fix that by changing the regular expression (outside the scope of this question), but I'll suggest using jq. It lets you use a command line utility to parse JSON data. Again, if you're connecting to that server with an IPv4 address, then this change won't be needed (until your ISP in the future updates you to an IPv6 address).

I therefore end up with

"%F{white}%M %n %y %j $(curl -s https://api.myip.com | jq .ip -r) %F{green}%2c%F{blue} [%f "
4
  • 1
    I assume the OP wants to update their IP address each time the prompt is shown, so running curl once when the shell starts may not be what they need. They might be better of with escaping the inner '...' when defining PROMPT (or use "..." there).
    – nohillside
    Commented Oct 30, 2020 at 6:31
  • Ah, good point. I would address that by escaping the evaluation with \$(curl..., and adding setopt prompt_subst, to allow ZSH to do evaluation each time the prompt is displayed. unix.stackexchange.com/a/297700/367868
    – Matthias
    Commented Oct 30, 2020 at 18:43
  • What about this: $(sysctl hw.ncpu | grep -o -E '[0-9]+'). Again, it works fine in the command prompt, but when inserted into my zsh-theme, it says. zsh: no matches found: [0-9]+ Commented Oct 30, 2020 at 23:35
  • Oops - I did it again. Ha. Yeah, it's the single quotes that is the problem both times. I was a bit dull there for a moment :) Commented Oct 31, 2020 at 0:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .