├── .travis.yml ├── README.md ├── LICENSE.md └── bush /.travis.yml: -------------------------------------------------------------------------------- 1 | language: bash 2 | 3 | addons: 4 | apt: 5 | sources: 6 | - debian-sid 7 | packages: 8 | - shellcheck 9 | 10 | os: 11 | - linux 12 | 13 | script: 14 | - shellcheck bush 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bush 2 | 3 | [![Build Status](https://travis-ci.org/dylanaraps/bush.svg?branch=master)](https://travis-ci.org/dylanaraps/bush) 4 | 5 | This is an experiment to see how many standard tools and functions we can re-implement in pure bash. No external processes are used, only shell builtins. Bush is not meant to be used for anything serious and there's probably edge cases and bugs throughout. 6 | 7 | Feel free to hack around with this, that's the entire point anyway. ¯\\\_(ツ)\_/¯ 8 | 9 | 10 | ## How to use this? 11 | 12 | Source the file and the functions will be available. To uninstall it just open another shell or close your terminal. 13 | 14 | ```sh 15 | source ~/path/to/bush/bush 16 | ``` 17 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Dylan Araps 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /bush: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Standard functions. 4 | 5 | basename() { 6 | # Get the basename of a path. 7 | # Usage: basename "path" 8 | path="${1%/}" 9 | printf "%s\\n" "${path##*/}" 10 | } 11 | 12 | dirname() { 13 | # Get the dirname of a path. 14 | # Usage: dirname "path" 15 | printf "%s\\n" "${1%/*}/" 16 | } 17 | 18 | strip() { 19 | # Strip characters from a string. 20 | # Usage: strip "string" "chars to remove" 21 | printf "%s\\n" "${1//$2}" 22 | } 23 | 24 | rstrip() { 25 | # Strip characters from the end of a string. 26 | # Usage: rstrip "string" "chars to remove" 27 | printf "%s\\n" "${1%%$2}" 28 | } 29 | 30 | lstrip() { 31 | # Strip characters from the start of a string. 32 | # Usage: lstrip "string" "chars to remove" 33 | printf "%s\\n" "${1##$2}" 34 | } 35 | 36 | # shellcheck disable=SC2086,SC2048 37 | trim() { 38 | set -f 39 | set -- $* 40 | printf "%s\\n" "$*" 41 | set +f 42 | } 43 | 44 | trim_quotes() { 45 | # Trim quotes from a string. 46 | # Usage: trim_quotes "string" 47 | trim="${1//\'}" 48 | printf "%s\\n" "${trim//\"}" 49 | } 50 | 51 | lower() { 52 | # Lowercase a string. 53 | # Usage: lower "string" 54 | printf "%s\\n" "${1,,}" 55 | } 56 | 57 | upper() { 58 | # Uppercase a string. 59 | # Usage: lower "string" 60 | printf "%s\\n" "${1^^}" 61 | } 62 | 63 | date() { 64 | # Output the date/time. 65 | # Usage: date "format" 66 | printf "%($1)T\\n" 67 | } 68 | 69 | cat() { 70 | # Output the contents of files. 71 | # Usage: cat "file" "file" "file" 72 | for file in "$@"; do 73 | printf "%s\\n" "$(<"$file")" 74 | done 75 | } 76 | 77 | head() { 78 | # Output n lines from the start of a file. 79 | # Usage: head "n" "file" 80 | mapfile -tn "$1" line < "${2:-/etc/os-release}" 81 | printf "%s\\n" "${line[@]}" 82 | } 83 | 84 | tail() { 85 | # Output n lines from the end of a file. 86 | # Usage: tail "n" "file" 87 | mapfile -tn 0 line < "${2:-/etc/os-release}" 88 | printf "%s\\n" "${line[@]: -$1}" 89 | } 90 | 91 | lines() { 92 | # Count the lines in a file. 93 | # Usage lines "file" 94 | mapfile -tn 0 lines < "$1" 95 | printf "%s\\n" "${#lines[@]}" 96 | } 97 | 98 | reverse_array() { 99 | # Reverse an array. 100 | # Usage: reverse_array "array" 101 | 102 | # extdebug reverses arguments for some reason. 103 | # We can use this to reverse an array. 104 | shopt -s extdebug 105 | f()(printf "%s " "${BASH_ARGV[@]}"); f "$@" 106 | shopt -u extdebug 107 | 108 | printf "\\n" 109 | } 110 | 111 | rgb_to_hex() { 112 | # Convert an rgb color to hex. 113 | # Usage: rgb_to_hex "r" "g" "b" 114 | printf "#%02x%02x%02x\\n" "$1" "$2" "$3" 115 | } 116 | 117 | hex_to_rgb() { 118 | # Convert a hex color to rgb. 119 | # Usage: hex_to_rgb "color" 120 | r="${1:1:2}" 121 | g="${1:3:2}" 122 | b="${1:5:6}" 123 | 124 | printf "%s %s %s\\n" "$((16#$r))" "$((16#$g))" "$((16#$b))" 125 | } 126 | 127 | uptime() { 128 | # Output the uptime in a pretty format. 129 | # Usage: uptime 130 | seconds="$(< /proc/uptime)" 131 | seconds="${seconds/.*}" 132 | 133 | days="$((seconds / 60 / 60 / 24)) days" 134 | hours="$((seconds / 60 / 60 % 24)) hours" 135 | mins="$((seconds / 60 % 60)) minutes" 136 | 137 | # Remove plural if < 2. 138 | ((${days/ *} == 1)) && days="${days/s}" 139 | ((${hours/ *} == 1)) && hours="${hours/s}" 140 | ((${mins/ *} == 1)) && mins="${mins/s}" 141 | 142 | # Hide empty fields. 143 | ((${days/ *} == 0)) && unset days 144 | ((${hours/ *} == 0)) && unset hours 145 | ((${mins/ *} == 0)) && unset mins 146 | 147 | uptime="${days:+$days, }${hours:+$hours, }${mins}" 148 | uptime="${uptime%', '}" 149 | 150 | printf "%s\\n" "up ${uptime:-${seconds} seconds}" 151 | } 152 | 153 | colors() { 154 | # Display the terminal palette. 155 | # Usage: colors 156 | for i in {0..15}; do 157 | ((i%8==0)) && printf "\\n" 158 | printf "%b" "\\e[48;05;${i}m \\e[0m" 159 | done 160 | 161 | printf "\\n\\n" 162 | } 163 | --------------------------------------------------------------------------------