Skip to content

Instantly share code, notes, and snippets.

@w495
Created December 8, 2025 19:59
Show Gist options
  • Select an option

  • Save w495/78a86bad4ac67dd980540772b34bad18 to your computer and use it in GitHub Desktop.

Select an option

Save w495/78a86bad4ac67dd980540772b34bad18 to your computer and use it in GitHub Desktop.
rand_t — Generates random text based on /dev/urandom and tr. This is function for ~/.bashrc
#!/usr/bin/env bash
# shellcheck enable=all
rand_t(){
local options
options=$(getopt -n 'rand_t' -o 'hs:w:l:a:x:y:c:m:n:' \
--long 'help,set:,width:,lines:' -- "${@}")
eval set -- "${options}"
local set='A-Za-z0-9_\-'
local -i width=60
local -i lines=10
while [[ -n ${options} ]]; do
case ${1} in
-h | --help)
echo 'Generates random text.' >&2
echo " -a | -c | -s | --set <SET> — string of characters. '${set}' is default">&2
echo " -x | -m | -w | --width <NUM> — number chars in line. ${width} is default">&2
echo " -y | -n | -l | --lines <NUM> — number lines instead. ${lines} is default">&2
echo " -v | -h | --help — shows this text & example">&2
echo
shift 1
;;
-a | -c | -s | --set)
set="${2}"
shift 2
;;
-x | -m | -w | --width)
width="${2}"
shift 2
;;
-y | -n | -l | --lines)
lines="${2}"
shift 2
;;
'--' | '')
shift 1
break
;;
*)
echo "Unknown parameter '${1}'." >&0
shift 1
;;
esac
done
local rest="${*// /}"
if [[ -n "${rest}" ]]; then
set="${rest}"
fi
< /dev/urandom tr -dc "${set}" \
| fold --width "${width}" \
| head --lines "${lines}" \
|| true
}
randtext(){
rand_t "${@}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment