My googlefu is failing me. I have a bunch of variables I read it from a CSV where I want to strip whitespace. I could
variable1="$(echo -e "${variable1}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
variable2="$(echo -e "${variable2}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
variable3="$(echo -e "${variable3}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
but then I'm copying in and editing this for every variable change. It seems to me, from other languages, there should be a way to do a simple for loop that applies to the variables as named and strips the whitespace, but I can't figure out exactly how to do that.
for $i in variable1 variable2 variable3
do
$i=$(echo -e "${$i}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
done
but this doesn't work as far as I can tell. What I want to do is loop over all the variables, strip whitespace, but not have to manually type out each variable name in the script.
Try the following:
# Sample values that need trimming.
variable1=' a '
variable2=' b c '
variable3=' sadf sdf sdf '
for name in ${!variable*}; do
# Trim leading and trailing whitespace.
read -r $name <<<"${!name}"
# Print result
echo "Variable $name now contains: [${!name}]"
done
The solution relies on variable indirection, the ability to refer to a variable indirectly, via another variable containing the target variable's name.
Note that variable indirection has its pitfalls (see link) and that there often better solutions, e.g., through use of arrays.
${!variable*}
expands to the names of all defined variables whose name starts with variable
.${!name}
expands to the value of the variable whose name is stored in variable $name
.read -r
, by virtue of reading into a single variable, implicitly trims leading and trailing whitespace.
-r
ensures that \
chars. in the input are left alone, which is typically what you want.