Last active
September 29, 2022 00:42
-
-
Save jhyland87/4a7d2d9cef9e3aa7f2eccaa4a68a4330 to your computer and use it in GitHub Desktop.
Example of a function that accepts input from stdin or as arguments
This file contains 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
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