Skip to content

Instantly share code, notes, and snippets.

@tcely
Created September 22, 2020 00:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcely/c667b73e68f82cd8d18e06493b68839d to your computer and use it in GitHub Desktop.
Save tcely/c667b73e68f82cd8d18e06493b68839d to your computer and use it in GitHub Desktop.
A simple echo using the shell printf for consistent behavior
#!/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