0

I have some output files like output1.bin, output2.bin and I need to add them to the list output_files. It works as expected when output files exists, but it adds "output*.bin" in the list variable if output1.bin, output2.bin does not exists. I expected that if "${outputdir}"/output*.bin doesn't match any file it should not even iterate through it i.e. I expeted the list to be empty but it adds output*bin name to it. Can someone explain the behavior and the fix ?

for bin_file in "${outputdir}"/output*.bin; do
    output_files+=("${bin_file}")
done
Cool Camel
  • 57
  • 6
  • 5
    `shopt -s nullglob`. See: [Filename Expansion](https://www.gnu.org/software/bash/manual/html_node/Filename-Expansion.html) – jhnc Feb 17 '23 at 14:18
  • 5
    BTW: You don't need the loop at all. Use `output_files+=("$outputdir"/output*.bin)` with nullglob enabled as suggested by jhnc. – Socowi Feb 17 '23 at 14:43
  • BTW, notice the titles on the duplicate questions this is closed with -- they're both much more descriptive in terms of letting someone know what the actual problem is just from reading that one-line title. Try to do that in your own questions going forward; "not working as expected" could mean anything. – Charles Duffy Feb 17 '23 at 17:16

0 Answers0