File: tldr.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright (c) 2026 pacman64 6 # 7 # Permission is hereby granted, free of charge, to any person obtaining a copy 8 # of this software and associated documentation files (the "Software"), to deal 9 # in the Software without restriction, including without limitation the rights 10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 # copies of the Software, and to permit persons to whom the Software is 12 # furnished to do so, subject to the following conditions: 13 # 14 # The above copyright notice and this permission notice shall be included in 15 # all copies or substantial portions of the Software. 16 # 17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 # SOFTWARE. 24 25 26 # tldr [options...] [tools...] 27 # 28 # Lookup examples from the `tldr` set, showing how to use command-line tools. 29 # When not given options to look into OS-specific entries, all entries are 30 # looked-up from the `common` set. 31 # 32 # All options, which are also available starting with double-dashes 33 # 34 # -f, -free, -freebsd look for freebsd-specific entries by default 35 # -h, -help show this help message 36 # -l, -linux look for linux-specific entries by default 37 # -m, -mac, -macos look for (modern) macos-specific entries by default 38 # -o, -open, -openbsd look for openbsd-specific entries by default 39 # -w, -win, -windows look for windows-specific entries by default 40 41 42 case "$1" in 43 -h|--h|-help|--help) 44 awk '/^# +tldr /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 45 exit 0 46 ;; 47 -f|--f|-free|--free|-freebsd|--freebsd) kind="freebsd"; shift ;; 48 -l|--l|-linux|--linux) kind="linux"; shift ;; 49 -m|--m|-mac|--mac|-macos|--macos|-macosx|--macosx|-osx|--osx) 50 kind="osx" && shift 51 ;; 52 -o|--o|-open|--open|-openbsd|--openbsd) kind="openbsd"; shift ;; 53 -w|--w|-win|--win|-windows|--windows) kind="windows"; shift ;; 54 --) shift ;; 55 -*) 56 printf "unsupported option '%s'\n" "$1" >&2 57 exit 1 58 ;; 59 esac 60 61 [ "$1" = '--' ] && shift 62 63 if [ $# -eq 0 ]; then 64 awk '/^# +tldr /, /^$/ { gsub(/^# ?/, ""); print }' "$0" >&2 65 exit 1 66 fi 67 68 base='https://raw.githubusercontent.com/tldr-pages/tldr/main/pages' 69 kind="${kind:-common}" 70 71 options='-MKiCRS' 72 if [ $# -eq 1 ]; then 73 options='-MKiCRS --header=1' 74 fi 75 76 got=0 77 for arg in "$@"; do 78 [ "${got}" -gt 0 ] && printf "\n" 79 got=1 80 81 # auto-complete partial names 82 if echo "${arg}" | grep -q -v / ; then 83 arg="${kind}/${arg}" 84 fi 85 86 printf "\033[7m# %-80s\033[0m\n" "${arg}" 87 88 curl --silent --show-error "${base}/${arg}.md" 2>&1 | awk ' 89 NR == 1 && /^404: / { 90 printf "\x1b[38;2;204;0;0m%s\x1b[0m\n", $0 91 next 92 } 93 94 NR == 1 { next } 95 96 NR <= 2 || /^>/ { 97 printf "\x1b[38;2;164;164;164m%s\x1b[0m\n", $0 98 next 99 } 100 101 /^`/ { 102 s = substr($0, 2, length - 2) 103 printf "`\x1b[48;2;224;224;224m%s\x1b[0m`\n", s 104 next 105 } 106 107 1 108 ' 109 done | { less ${options} 2> /dev/null || less -RIMS 2> /dev/null || cat; }