0

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.)

2
  • 1
    ls "${arguments[@]}"
    – vgersh99
    Commented Jun 9, 2020 at 16:07
  • Sometimes it is really hard to search for things. If my duplicate question makes it more likely that someone can find an answer to their searches, I'm happy.
    – thoni56
    Commented Jun 10, 2020 at 8:48

0

Browse other questions tagged or ask your own question.