Skip to content

Instantly share code, notes, and snippets.

@goldie-lin
Last active September 29, 2022 00:39
Show Gist options
  • Save goldie-lin/a9b275d87137e64da735 to your computer and use it in GitHub Desktop.
Save goldie-lin/a9b275d87137e64da735 to your computer and use it in GitHub Desktop.
bash: trim leading & trailing whitespace chars in a variable
#!/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