Can bash variables be multiline?… here is a solution to the problem.
Can bash variables be multiline?
I wrote a script to list path entries and filter them by keywords. I managed to get it to work, but I had to write the list in a temporary file because I couldn’t store it in memory while keeping line breaks. Is there a better/more suitable solution?
#! /bin/bash
tr ':' '\n' <<< $PATH > tmp
if (( $# > 0 )); then
for W
do
grep $W < tmp
done
else
cat tmp
fi
rm tmp
Solution
Yes, variables can contain line breaks. You need to reference the variable when you use it. Your script can be written as:
tmp=$(tr ':' '\n' <<< $PATH)
if (( $# > 0 )); then
for W; do
grep $W <<< "$tmp"
done
else
echo "$tmp"
fi