0

Please assume that we are now within a directory containing 13 files that were named as follows:

p1 p2 p3 ... p11 p12 p13

Through bash, when we execute the following command:

for i in p*; do echo $i; done

The star * wildcard seems to be expanded without an ascending order:

p1
p10
p11
...
p7
p8
p9

Yet for example, the following piece of commands expand the numerical values in an ascending order:

for i in p{1..13}; do echo $i; done

Is there any bash settings to alter this default star * wildcard behaviour?

If not, could you please tell us what is the motivation behind this preference for such an expansion?

PS: For a similar question, please refer to: Does the Bash star * wildcard always produce an (ascending) sorted list?. Yet herein I wonder if we can change the default behaviour of bash, and if so, how?

  • 2
    Let us know why you think lexicographical sorting of these files is counter-intuitive. Also consider that filenames may be on any form, e.g. file-1-1001, file-1-1002 ... file-1-1999 or from file-A-1001 through to file-Z-1001 followed by file-A-1002 etc. What would be the "intuitive" numerical sorting for these? – Kusalananda Mar 28 '18 at 10:35
  • 1
    bash has no setting to alter the order of glob expansion. Those who can't/don't want to use zsh have two options: either rename the files (zero-padding the numbers) or resort to contortions like for f in p? p?? p???; do... – don_crissti Mar 28 '18 at 10:52
  • @Kusalananda Your observation is definitely correct. The word 'intuitive' is not quantifiable, yet a subjective object (but, I tried to cover it with 'IMHO' in my defence). Therefore, I edited my question, and removed those words. Thanks. – Herpes Free Engineer Mar 28 '18 at 11:02
  • 1
    The answer is essentially no. More info is given in the duplicate. – terdon Mar 28 '18 at 11:14

0 Answers0