I want DIR to loop through all the arguments (with space and ""). But this code do not recognize, "cd ef" as one. Dividing them into two. How can I do that?
#test.sh
echo Number of arguments: $#
if [ $# -eq 0 ]
then
echo "No arguments"
DIR=.
else
DIR="$*"
fi
echo DIR =$DIR
for arg in $DIR;
do
echo $arg;
done
Command
#bash test.sh ab "cd ef"
Output, Number of arguments: 2
But, enumerating 3 elements in the loop as ab, cd, and ef.
I know, I can do,
for arg in "$*";
But that not the solution in this case. Can I use DIR for this purpose? The platform is OSX Mavericks shell.
$*is almost never appropriate for use -- it concatenates arguments with the first character ofIFS, meaning that if any argument happens to contain the first character of IFS, it's impossible to distinguish between the characters delimiting the arguments and the characters within the arguments.