Cheatsheet script for displaying Linux command examples
You can use cheat sh web service to show cheatsheets for all kind of commands. Just replace the command name: curl -s cheat.sh/date
. I also wrote a a simple script with filename being just a question mark to get a working command as ?
, that shows all commands in fzf
menu if no argument is given or shows the cheatsheet in the less pager if command name is given.
Usage:
<span style="color:#323232;">?
</span><span style="color:#323232;">? -l
</span><span style="color:#323232;">? date
</span><span style="color:#323232;">? grep
</span>
<span style="color:#323232;">#!/bin/env bash
</span><span style="color:#323232;">
</span><span style="color:#323232;">cheat='curl -s cheat.sh'
</span><span style="color:#323232;">menu='fzf --reverse'
</span><span style="color:#323232;">pager='less -R -c'
</span><span style="color:#323232;">cachefile_max_age_hours=6
</span><span style="color:#323232;">
</span><span style="color:#323232;"># Path to temporary cache file. If your Linux system does not support /dev/shm
</span><span style="color:#323232;"># or if you are on MacOS, then change the path to your liking:
</span><span style="color:#323232;">cachefile='/dev/shm/cheatlist' # GNU+LINUX
</span><span style="color:#323232;"># cachefile="${TMPDIR}/cheatlist" # MacOS/Darwin
</span><span style="color:#323232;">
</span><span style="color:#323232;"># Download list file and cache it.
</span><span style="color:#323232;">listing () {
</span><span style="color:#323232;"> if [ -f "${cachefile}" ]
</span><span style="color:#323232;"> then
</span><span style="color:#323232;"> local filedate=$(stat -c %Y -- "${cachefile}")
</span><span style="color:#323232;"> local now=$(date +%s)
</span><span style="color:#323232;"> local age_hours=$(( (now - filedate) / 60 / 60 ))
</span><span style="color:#323232;"> if [[ "${age_hours}" > "${cachefile_max_age_hours}" ]]
</span><span style="color:#323232;"> then
</span><span style="color:#323232;"> ${cheat}/:list > "${cachefile}"
</span><span style="color:#323232;"> fi
</span><span style="color:#323232;"> else
</span><span style="color:#323232;"> ${cheat}/:list > "${cachefile}"
</span><span style="color:#323232;"> fi
</span><span style="color:#323232;"> cat -- "${cachefile}"
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">case "${1}" in
</span><span style="color:#323232;"> '')
</span><span style="color:#323232;"> if selection=$(listing | ${menu})
</span><span style="color:#323232;"> then
</span><span style="color:#323232;"> ${cheat}/"${selection}" | ${pager}
</span><span style="color:#323232;"> fi
</span><span style="color:#323232;"> ;;
</span><span style="color:#323232;"> '-h')
</span><span style="color:#323232;"> ${cheat}/:help | ${pager}
</span><span style="color:#323232;"> ;;
</span><span style="color:#323232;"> '-l')
</span><span style="color:#323232;"> listing
</span><span style="color:#323232;"> ;;
</span><span style="color:#323232;"> *)
</span><span style="color:#323232;"> ${cheat}/${@} | ${pager}
</span><span style="color:#323232;"> ;;
</span><span style="color:#323232;">esac
</span>
![](https://kbin.life/media/cache/resolve/entry_thumb/38/05/3805d297c498e93dc92bd3064322b192861ba99ef51d04585cb781f025743578.png)