On 17.07.2017 12:37, SF Markus Elfring wrote:
A corresponding result could be achieved by using a subshell (which I
would
like to avoid for this use case) for a command like “(cd ${my_dir} &&
ls *txt)”.
If you want to capture these strings into a variable, you can't really
avoid a sub-process.
I looked at a programming interface like the function “opendir”.
I imagine that there are more advanced possibilities to improve the
software
run time characteristics for this use case.
Well, if it has to be fast, perhaps don't write the code in the shell
language.
Even an interpreted scripting language that can do string handling
without
resorting to fork()-based command substitution will beat the shell at
many
tasks.
it can be done like this:
for name in dir/*txt ; do
echo ${name#dir/}
done
I would like to avoid such an operation “Remove matching prefix
pattern” generally.
If the desired file lists contain only basenames, extra prefixes do not
need
to be deleted.
I.e. we can use the basename function:
for name in dir/*txt; do
basename "$name"
done
prints the basenames of the matching files, one per line.