How to retrieve all names from one directory?
The directory contains names such as :
.foo bar
.10
10.foo bar
foo bar.foo bar
I tried
for item in $(ls "/opt/$PATH")
do
echo "$item"
done
Will anyone advise? Thank you.
How to retrieve all names from one directory?
The directory contains names such as :
.foo bar
.10
10.foo bar
foo bar.foo bar
I tried
for item in $(ls "/opt/$PATH")
do
echo "$item"
done
Will anyone advise? Thank you.
Don't parse the output of ls
. Use globs, for example:
for item in "/opt/$path"/*; do echo "$item"; done
If you want just the filename without absolutely path, you have several options, for example:
for item in "/opt/$path"/*; do basename "$item"; done
or:
(cd "/opt/$path" && for item in *; do echo "$item"; done)
If you want to include hidden files too, you can set dotglob
option before the above commands:
shopt -s dotglob
As a side note, the use of "/opt/$PATH"
is extremely suspicious.
PATH
is an extremely important variable in the shell, containing a colon-separated list of directories in which the shell looks for commands.
It's unlikely that "/opt/$PATH"
will have the effect that you want.
Do not use this variable for other purposes than it's designed.