Created
January 29, 2024 08:04
-
-
Save tenfyzhong/a6c250400fe11049ba592df2a00173c8 to your computer and use it in GitHub Desktop.
Trim leading and trailing space in bash
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
trim() { | |
local var="$*" | |
# remove leading whitespace characters | |
var="${var#"${var%%[![:space:]]*}"}" | |
# remove trailing whitespace characters | |
var="${var%"${var##*[![:space:]]}"}" | |
printf '%s' "$var" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment