Created
September 22, 2020 00:04
-
-
Save tcely/c667b73e68f82cd8d18e06493b68839d to your computer and use it in GitHub Desktop.
A simple echo using the shell printf for consistent behavior
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env sh | |
secho() { | |
_arg="${1}"; | |
_fmt='%s'; | |
_sentinel='--'; | |
case "${_arg}" in | |
(-e|-en|-ne) _fmt='%b'; shift ;; | |
(-n|-En|-nE) shift ;; | |
(-E|-Ee|-eE) shift ;; | |
(-enE|-eEn|-nEe|-neE|-Een|-Ene) shift ;; | |
esac; | |
while [ "${#}" -gt 0 ]; do | |
[ -n "${_sentinel}" ] && | |
[ "${1}" = "${_sentinel}" ] && { | |
_sentinel=''; | |
shift; | |
continue; | |
}; | |
printf -- "${_fmt}" "${1}"; | |
[ "${#}" -gt 1 ] && { | |
printf -- '%c' "${IFS- }" | |
}; | |
shift; | |
done; | |
case "${_arg}" in | |
(-n|-en|-ne|-En|-nE) : ;; | |
(-enE|-eEn|-nEe|-neE|-Een|-Ene) : ;; | |
(*) printf -- '\n' ;; | |
esac; | |
unset -v _arg _fmt _sentinel; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment