2

In Terminal, I prefer to use bash as my default shell. Issuing the command touch File_{01..10}.txt produces these results:

File_1.txt, File_10.txt, File_2.txt, File_3.txt, File_4.txt, File_5.txt, File_6.txt, File_7.txt, File_8.txt, File_9.txt

As you can see, there are no leading zeros in the file names.

However, using zsh, the same command touch File_{01..10}.txt produces these results:

File_01.txt, File_02.txt, File_03.txt, File_04.txt, File_05.txt, File_06.txt, File_07.txt, File_08.txt, File_09.txt, File_10.txt

As you can see, zsh produces the desired results with the leading zeros.

Is there anyway I can configure bash to produce the same results as zsh for use with the touch command?

2
  • Some options for adding leading zeros in sequences are discussed in this question. Note that one possible answer is installing a modern version of bash.
    – Gairfowl
    May 2, 2022 at 7:27
  • This is not related to touch, but to the way your shells do the brace expansion. You can also test it by doing a echo {01..10}. This may depend on the shell version: My bash (4.4) also produces the expansion with leading zeroes, like zsh. May 10, 2022 at 7:24

2 Answers 2

1

The Bash version included with macOS is quite old (3.2.57 as of Monterey 12.3.1) and doesn't honor leading zeros when expanding {01..10}, but newer versions of Bash do (from man bash):

A sequence expression takes the form {x..y[..incr]}, where x and y are either integers or single characters, and incr, an optional increment, is an integer. When integers are supplied, the expression expands to each number between x and y, inclusive. Supplied integers may be prefixed with 0 to force each term to have the same width. When either x or y begins with a zero, the shell attempts to force all generated terms to contain the same number of digits, zero-padding where necessary.

I'd recommend that you install Homebrew and then Bash (5.1.16 as of this writing), and configure Terminal to use that version instead (correct the path below accordingly):

enter image description here

2
  • 1
    Perfect… works like a charm. Does this mean that when I'm creating and saving shell scripts, the shebang now should be #! /usr/local/Cellar/bash/5.1.16/bin/bash?
    – wch1zpink
    May 2, 2022 at 23:42
  • 1
    Good question... #! /usr/local/Cellar/bash/5.1.16/bin/bash will break when bash is updated. I would either recommend #!/usr/local/bin/bash or #!/usr/bin/env bash. The former points to a symlink to the current version of bash, the latter selects bash according to PATH, which is a more robust approach but, for it to work as expected, you must edit PATH in .bash_profile and prepend /usr/local/bin to it: export PATH=/usr/local/bin:$PATH.
    – jaume
    May 3, 2022 at 5:30
2

Without installing a package manager and then a newer version of your shell, you could work with the tools you have. Using the builtin command printf you could-

touch $(printf "file_%02d.txt " {1..10})

You could also use the external command jot-

touch $(jot -s " " -w 'file_%02d.txt' 10)

Now if you wanted to use the bash5 sequence expression {START..END[..INCREMENT]} then -

jot -s " " -w 'file_%02d.txt' 4 2 10

where 4 equal the number of times printed, 2 equals the increment, and 10 equal the maximum value.

Of course after all, you could use zsh within your "default" shell to do the lifting-

zsh -c 'touch file_{01..10}.txt'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .