Brain Phrye

code cooking diy fiction personal photos politics reviews tools 


Sarcasm (updated)

[ Listen to article ]

After years of complaining that the internet lacked a sarcasm font it seems we’ve decided on random mixed case for denoting sarcasm.

Clearly we need a shell script to do this.

Or rather, “cLearLY wE neeD a SheLl sCrIpT to do tHiS.”

But I do find it useful, hence this recent addition to my home dir - ~/bin/sarcasm:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#!/usr/bin/env bash

declare -a text
text=($( grep -o . <<< "${*^^*}" | sed 's/ /__/') )
sarcasm=
for c in "${text[@]}"; do
  if [[ "$c" == __ ]]; then
    c=" "
  elif [[ $((RANDOM % 5)) -ge 2 ]]; then
    c="${c,*}"
  fi
  sarcasm="$sarcasm$c"
done
printf '%s\n' "$sarcasm"

In doing this I learned two new tricks. You can change case just with bash by doing ${var^[a-z]} or ${var,[A-Z]}. By doing the caret or comma twice you can affect all matches.

I also learned grep -o which will emit matched text, not matched lines. So echo foo bar foo | grep -o foo will emit two foo’s which might be a good way to avoid sed in some circumstances. Not sure if this is a GNU grep extension though.

No matter what language I code in, I keep learning new tricks.

Edit: Fixed the bug where I’d reversed the case matching. I realised I could just replace the [a-z] syntax with just a * - it takes globs not REs.