The Hamish
The Hamish

Reputation: 31

Build an array from directory listing with double quotes using bash

I have a directory of files and I want to build an array of them and double quote them

file1.json file2.json

if I declare my array and do a directory listing:

declare -a array=("$(ls)")

echo ${array[@]}

I get

file1.json file2.json

how do I make the array so that when I output it - it is

"file1.json" "file2.json"

Upvotes: 0

Views: 165

Answers (1)

ruakh
ruakh

Reputation: 183446

I think your best bet is to use printf to print each one with double-quotes:

array=(*)
printf '"%s" ' "${array[@]}"

Upvotes: 2

Related Questions