2

I want to echo a clickable URL on ZSH/Terminal. The URL corresponds to a Kibana-query, which means it includes lots of funky characters. For technical reasons, I cannot use "shortened" Kibana links.

read -r the_url << \EOF
https://log-service.com/app/discover#/?_g=(filters:!(),refreshInterval:(pause:!t,value:0),time:(from:now-1h,to:now))&_a=(columns:!(correlation_id,application,level,description),filters:!(('$state':(store:appState),meta:(alias:!n,disabled:!f,index:'*:log-2',key:correlation_id,negate:!f,params:(query:hello-there),type:phrase),query:(match_phrase:(correlation_id:hello-again)))),index:'*:log-2',interval:auto,query:(language:kuery,query:''),sort:!(!('@timestamp',desc)))
EOF
echo "logs at:"
echo "$the_url"

The problem is: When I try to command-click the link, it is only partly interpreted as hyperlink. As a result, only part of the url is passed to the browser, and Kibana does not load properly. I am well below the 1000 characters URL-limit of some browsers.

Is there a way I can make ZSH + Terminal interpret my URL correctly?

1
  • I arrived wondering if there were terminal escape codes to echo to the terminal to show a simple, clickable link — I didn't notice at first the question said to use Command-click. Command-click actually does not work for me, even for simple links (Monterey), but the context-menu does recognize plain-text links, when activated anywhere on the link. (No need to select.) Commented Aug 6 at 19:01

1 Answer 1

3

A colleague found a workaround: URL-encode those parts that make the terminal trip up. (URL-encoding everything does not work, since then Kibana cannot read it anymore).

#!/bin/bash

read -r the_url << \EOF
https://log-service.com/app/discover#/?_g=(filters:!(),refreshInterval:(pause:!t,value:0),time:(from:now-1h,to:now))&_a=(columns:!(correlation_id,application,level,description),filters:!(('$state':(store:appState),meta:(alias:!n,disabled:!f,index:'*:log-2',key:correlation_id,negate:!f,params:(query:hello-there),type:phrase),query:(match_phrase:(correlation_id:hello-again)))),index:'*:log-2',interval:auto,query:(language:kuery,query:''),sort:!(!('@timestamp',desc)))
EOF

IFS='=' read -ra parts <<< "$the_url"

for i in "${parts[@]}"
do
   if [[ -z $url ]]; then
   # this is the first part of the URL, we want to keep it as-is
      url="$i"
   else
      # urlencode that part of the URL and append with an '='
      i=${i//\'/\%27}
      i=${i//\(/\%28}
      i=${i//\)/\%29}
      i=${i//\*/\%2a}
      url="$url=$i"
   fi
done


echo "logs at:"
echo "$url"
1
  • Good solution. I was wondering if pbcopy would work with the non-encoded string…
    – bmike
    Commented Aug 8, 2022 at 16:08

You must log in to answer this question.

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