Last active
September 29, 2022 00:39
-
-
Save goldie-lin/a9b275d87137e64da735 to your computer and use it in GitHub Desktop.
bash: trim leading & trailing whitespace chars in a variable
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
#!/usr/bin/env bash | |
# Function definitions | |
# -------------------- | |
# trim leading & trailing whitespace chars | |
trim() { | |
local var="$1" | |
var="${var#"${var%%[![:space:]]*}"}" # trim leading whitespace chars | |
var="${var%"${var##*[![:space:]]}"}" # trim trailing whitespace chars | |
echo -n "$var" | |
} | |
# Script begin | |
# ------------ | |
# get variables | |
datetime1="$(adb shell date +%s)" | |
# trim variables | |
datetime1_trimmed="$(trim "${datetime1}")" | |
# print out for debug | |
echo "datetime1=[${datetime1}]" | |
echo "datetime1_trimmed=[${datetime1_trimmed}]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment