using "ls" and preserving the spaces in the resulting array

using thesaurus
using semicolons
another word for using something
useing or using
utilizing
using meaning in hindi
define utilize
using someone synonym

I am trying to read a directory with "ls" and do operations on it

directory example:

$ ls -1
x x
y y
z z

script file: myScript.sh

#!/bin/bash
files=(`ls -1`); 
for ((i=0; i<"${#files[@]}"; i+=1 )); do
    echo "${files[$i]}"
done

however, the output is

$ myScript.sh
x
x
y
y
z
z

yet if I define "files" in the following way

$ files=("x x" "y y" "z z")
$ for ((i=0; i<"${#files[@]}"; i+=1 )); do echo "${files[$i]}"; done
x x
y y
z z

How can I preserve the spaces in "files=(`ls -1`)"?

Using Synonyms, Using Antonyms, describing an action or state of affairs that was done repeatedly or existed for a period in the past. Define using. using synonyms, using pronunciation, using translation, English dictionary definition of using. v. used , us·ing , us·es v. tr. 1. To put into service

It seems the main conclusion is not to use ls. Back in Pleistocene age of Unix programming, they used ls; however, these days, ls is best-restricted to producing human-readable displays only. A robust script for anything that can be thrown at your script (end lines, white spaces, Chinese characters mixed with Hebrew and French, or whatever), is best achieved by some form of globbing (as recommended by others here BashPitfalls).

#!/bin/bash
 for file in ./*; do
    [ -e "${file}" ] || continue
    # do some task, for example, test if it is a directory.
    if [ -d "${file}" ]; then
        echo "${file}"
    fi
done

The ./ is maybe not absolutely necessary, but it may help if the file begins with a "-", clarifying which file has the return line (or lines), and likely some other nasty buggers. This is also a useful template for specific files (.e.g, ./*.pdf). For example, suppose somehow the following files are in your directory: "-t" and "<CR>t". Then (revealing other issues with ls when using nonstandard characters)

$ ls
-t  ?t
$ for file in *; do ls "${file}"; done
-t  ?t
?t

whereas:

$ for file in ./*; do ls "${file}"; done
./-t
./?t

also

$ for file in ./*; do echo "${file}"; done
./-t
./
t

A workaround with POSIX commands can be achieved by --

$ for file in *; do ls -- "${file}"; done # work around
-t
?t

Use, be or become familiar with someone or something through experience. Using: to put into action or service. Synonyms: applying, employing, exercising… Find the right word. SINCE 1828. GAMES; BROWSE THESAURUS; WORD OF THE DAY; WORDS AT

Try this:

eval files=($(ls -Q))

Option -Q enables quoting of filenames. Option -1 is implied (not needed), if the output is not a tty.

Using Synonyms, Using Antonyms, to employ for some purpose; put into service; make use of: to use a knife. to avail oneself of; apply to one's own purposes: to use the facilities. to expend or consume in use: We have used the money provided. to treat or behave toward: He did not use his employees with much consideration. Use definition is - to put into action or service : avail oneself of : employ. How to use use in a sentence. Synonym Discussion of use.

Using, c : a method or manner of employing or applying something gained practice in the use of the camera. 2a : the privilege or benefit of using something gave him� using statement (C# Reference) 05/29/2020; 4 minutes to read +7; In this article. Provides a convenient syntax that ensures the correct use of IDisposable objects. Beginning in C# 8.0, the using statement ensures the correct use of IAsyncDisposable objects.

USE (verb) definition and synonyms, 23 synonyms of using from the Merriam-Webster Thesaurus, plus 30 related words, definitions, and antonyms. Find another word for using. The reason for the using statement is to ensure that the object is disposed as soon as it goes out of scope, and it doesn't require explicit code to ensure that this happens. As in Understanding the 'using' statement in C#, the C# compiler converts using (MyResource myRes = new MyResource()) { myRes.DoSomething(); }

USING, Define using. using synonyms, using pronunciation, using translation, English dictionary definition of using. v. used , us�ing , us�es v. tr. 1. To put into service or� The scope of a using directive is limited to the file in which it appears. The using directive can appear: At the beginning of a source code file, before any namespace or type definitions. In any namespace, but before any namespace or types declared in this namespace.

Comments
  • for filename in *; do echo "$filename"; done? Or array=( * ); for ((i = 0; i < ${#array[@]}; i++)); do echo "${array[i]}"; done? Never use for i in $(ls anything), see Bash Pitfalls #1
  • Why not parse ls?
  • OK, aside from adding the $ in "${array[$i]}", both examples in 2 worked. I still don't understand why "ls" can be made to deliver a clear delineation between file names, but the operation (ls -1) destroys it, yet writing "*" works without any problems. Anyway, thank you for the advice, it was better than the solution I finally found.
  • @DavidC.Rankin, ...bad filenames don't just come from random dingbats, they also come from bugs. You've heard my horror story about a buffer overflow in a Python library causing a Python script to create a filename that caused a shell script maintaining the same directory to delete *? Nasty things happen even in the absence of operator error.
  • Yes, @CharlesDuffy, that quip was meant more as a "tongue-in-cheek" bit of commiseration, rather than an all inclusive list of where nightmare filenames can come from. (I've even been the dingbat who on occasion has had a 3-liner go bad and and spew filenames I hard time getting rid of :) All in all, the lesson learned is to protect against the worst case rather than hope it doesn't exist on your box.
  • -print0 is severely under-rated!
  • BTW, if you only want directories, you can iterate over ./*/. With respect to ./, it doesn't make any difference for files that start with *s, but for files that start with -, the same issue can be worked around by using -- as an end-of-options argument, as given in Guideline 10 of the POSIX utility syntax guidelines (pubs.opengroup.org/onlinepubs/9699919799/basedefs/…, seek to the bottom of the page).
  • @CharlesDuffy I fixed the "*" issue in the text. The original reason for the script was that I didn't notice that the default mask on Windows 10 Ubuntu is 000. So I was cleaning up the permissions on some SW I was developing. Anyway, thank you for your patience and advice.
  • This only adds overhead over files=(*)
  • This doesn't work correctly with all possible filenames. sp=' '; touch "${sp}${sp}${sp}"; eval files=($( ls -Q )); ls "${files[@]}" will fail with ls: cannot access ' ': No such file or directory, to give you a concrete example.
  • ...and that's a harmless failure. touch '$(touch evil)' and then run eval files=($( ls -Q )) and you have a file named evil -- but you could have had something much more malicious if someone else had come up with that filename.
  • See Why should eval be avoided in Bash, and what should I use instead?.
  • Sorry you're getting down-voted to oblivion here, but please, please, please don't use eval when there's alternatives.