I have two examples of a bash script. All I do is just use sed to replace some strings in a file. The file is specified when the script is run. So, you would run it like ./replace_file.sh input_file.
hug="g h 7"
for elm in $hug
do
echo "replacing $elm"
sed -i'_backup' "s/$elm/BOO/g" $1
done
and
hug=('g' 'h' '7')
for elm in $hug
do
echo "replacing $elm"
sed -i'_backup' "s/$elm/BOO/g" $1
done
The first script above, echos the following:
replacing g
replacing h
replacing 7
But, the second one stops after replace g. It is unclear to me why. Can someone please shed some light?
Thank you.
clitypo sorry!for elm in ${hug[@]}in the case of the array... You can quote"${hug[@]}"to preserve whitespace in the elements (if they have any -- you don't) Attempting to call an array without braces, e.g.{...[@]}, you are simply referencing the first element of the array,'g'in your case.hug="g h 7"doesn't create an array at all; it just creates a string with whitespace in it.