Little Update: Just added an exit command after the help.
Here is another little script that nobody asked for. There are multiple ways to accomplish this, but I always forget how or which is the best way. Use tr
? Or sed
? When can I use the more efficient Bash substitutions instead, which are Bash integrated functionality of variables that saves me some extra calls. Also most solutions to title case will compress all spaces to single space after a word; not this "title" solution, which respects the spaces.
Use this like you would use grep
or tr
, which get input from stdin and output to stdout. There are no special options, only mode names without dashes. Multiple modes can be combined, but there is actually no reason to do so at the moment.
Example:
echo "Hello World, this is an EXAMPLE." | tocase toggle upper1
(Note, this Beehaw instance always replaces some characters and makes the below script unusable. Copy it from the linked script instead.)
#!/usr/bin/env bash
if [ "${#}" -eq 0 ] ; then
cat << EOF
usage: tocase option...
options:
upper all uppercase
upper1 upper first character
lower all lowercase
lower1 lower first character
toggle swap uppercase and lowercase
toggle1 swap upper and lower of first character
title upper first character and lower rest of each word
examples:
echo "Hello World, this is an EXAMPLE." | tocase toggle upper1
EOF
exit 0
fi
while IFS= read -r stdin ; do
for argument in "${@}" ; do
case "${argument}" in
upper) stdin="${stdin^^}" ;;
upper1) stdin="${stdin^}" ;;
lower) stdin="${stdin,,}" ;;
lower1) stdin="${stdin,}" ;;
toggle) stdin="${stdin~~}" ;;
toggle1) stdin="${stdin~}" ;;
title)
# Note: Many other solutions other than this sed command do not
# work on each word.
stdin="$(sed -r 's/\<./\U&/g' <<<"${stdin}")"
;;
esac
done
printf "%s\n" "${stdin}"
done