how a stray "j" ruined my evening
Some time ago I decided to wrangle a silly link shortener that I called shirts linkener, shirts for short.
There's nothing special about it in particular, rather it's in my run-once-forget-forever shell script that copies the generated links to a clipboard using wl-clipboard.
#!/bin/sh
shirt=$(curl -s -X POST "$(pass show shirts/url)" \
-H "Authorization: Bearer $(pass show shirts)" \
-H "Content-Type: application/json" \
-d "{\"url\": \"${1?missing url}\"}")
echo "$shirt" | jq
echo "$shirt" | jq -r '.short_url' | wl-copy
Nothing out of the ordinary here. It shortens the link and copies it to a clipboard. By then I have been hapilly using it for couple of months without an issue.
The fun began when I decided to try out gurk, a Signal TUI written in Rust. Those who know me, know my obsession with terminal interfaces so this was an obvious match.
One evening I was sharing a link to an image hosted on S3 when I noticed people were saying it shows 404 to them.
After pasting the link into Firefox, it worked. What is going on? I send them the link and it works now...
Time passes and I get the same responses from multiple friends - 404. After some time looking at the messages I notice a pattern - each url ends with "j".
Turns out I was copying the link with a \n at the end, which in most cases went unnoticed, but in ANSI newline delimiter is translated as "j", hence the stray jay at the end of each link.
After having a solid five minutes of immense shame I found that jq has --join-output argument, which prevents exactly my case:
--join-output / -j:
Like -r but jq won't print a newline after each output.
Moral of this story is that terminals are fun and shooting myself in the foot repeatedly acts as a great learning opportunity.
Some of the sources around control codes and escape sequences.
- https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797j
- https://cirw.in/blog/bracketed-paste
- https://jdhao.github.io/2021/02/01/bracketed_paste_mode
m.