I've been studying loops and here's some things
I've learned. I show the script ,
then the output in the terminal.
Here's a quick way of making a list for a loop
to use each word as an argument
Code: Select all
#!/bin/bash
LIST='apple orange pear plum'
for item in $LIST
do
echo "Argument value is: $item"
done
# ./script
Argument value is: apple
Argument value is: orange
Argument value is: pear
Argument value is: plum
Here is the usual way programmers make a list
Code: Select all
#!/bin/bash
LIST='apple
orange
pear
plum'
for item in $LIST
do
echo "Argument value is: $item"
done
# ./script
Argument value is: apple
Argument value is: orange
Argument value is: pear
Argument value is: plum
Double quotation marks are useless as this example
shows...
Code: Select all
#!/bin/bash
LIST=$(echo "ap ple" orange pear plum)
for item in $LIST
do
echo "Argument value is: $item"
done
# ./script
Argument value is: ap
Argument value is: ple
Argument value is: orange
Argument value is: pear
Argument value is: plum
Now I change the IFS (Internal Field Separator) to a comma
and the white space is protected
Code: Select all
#!/bin/bash
IFS=,
LIST='ap ple,orange,pear,plum'
for item in $LIST
do
echo "Argument value is: $item"
done
# ./script
Argument value is: ap ple
Argument value is: orange
Argument value is: pear
Argument value is: plum