Skip to content

Instantly share code, notes, and snippets.

@jhyland87
Last active September 29, 2022 00:42
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 jhyland87/4a7d2d9cef9e3aa7f2eccaa4a68a4330 to your computer and use it in GitHub Desktop.
Save jhyland87/4a7d2d9cef9e3aa7f2eccaa4a68a4330 to your computer and use it in GitHub Desktop.
Example of a function that accepts input from stdin or as arguments
function trim {
if [ $# -eq 0 ]
then
data=$(cat)
else
data="$*"
fi
data="${data#"${data%%[![:space:]]*}"}"
data="${data%"${data##*[![:space:]]}"}"
echo -n "$data"
}
# $ function trim {
# > if [ $# -eq 0 ]
# > then
# > data=$(cat)
# > else
# > data="$*"
# > fi
# >
# > data="${data#"${data%%[![:space:]]*}"}"
# > data="${data%"${data##*[![:space:]]}"}"
# >
# > echo "$data"
# > }
# $ trim " Hello World "
# Hello World
# $ echo " Hello World " | trim
# Hello World
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment