Thank you for sharing @williamwlk! Very valuable resource indeed.
My pleasure @Deanna :) and echo your comment (Very valuable resource indeed.).
Will
Beware of unquoted variable expansions shown as example on that page. For example,
for i in "${arrayName[@]}"; do echo $i done
Should be:
for i in "${arrayName[@]}"; do printf '%s\n' "$i" done
To see why, create a file named "file1" somewhere:
cd /tmp
touch file1
Then create an array like this (note that array element zero is the string "file*"; no filename expansion yet):
arrayName=('file*')
And then unsafely run the sample for loop; you'll get:
for i in "${arrayName[@]}"; do echo $i; done
file1
You "accidentally" ran into filename expansion (globbing) when weren't expecting it.
Next, consider using printf instead of echo. Load the array this way:
arrayName=('-e')
and then run the loop; even with proper quoting, you'll get:
for i in "${arrayName[@]}"; do echo "$i"; done
... with a blank line! Using printf instead gets you the actual data:
for i in "${arrayName[@]}"; do printf '%s\n' "$i"; done
-e
Hey, @Jeff_Schaller,
Well spotted! I have also noticed some weirdness such as:
And of course, whatever is already in the comments.
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.