In a script such as this
#! /bin/bash -i
arguments=(arg1 "arg2 has many spaces")
echo arguments count = ${#arguments[@]}
echo arguments = ${arguments[@]}
echo ls ${arguments[@]}
ls ${arguments[@]}
How can I keep arg2
as a single argument to ls
?
This is the actual output:
arguments count = 2
arguments = arg1 arg2 has many spaces
ls arg1 arg2 has many spaces
ls: arg1: No such file or directory
ls: arg2: No such file or directory
ls: has: No such file or directory
ls: many: No such file or directory
ls: spaces: No such file or directory
This shows that arguments
initially has 2 elements but when it is used as arguments in the ls
command the quotes are no longer there (obviously, since they were remove already when arguments
was initialized) so ls
sees 5 arguments.
How can I apply quoting to each of the array elements separately in order to give ls
two arguments by keeping the second element together?
(In the real case I don't know the actual content of the array and ls
is only an example which illustrates the problem.)