0

On my local machine, where I run CentOS 7.2, when I do this:

for i in {8..12}; do echo ${i}; done

I got:

8
9
10

But when I do this:

for i in {08..10}; do echo ${i}; done

I got:

08
09
10

But on server to which I have access, which runs CentOS 5.6, running both commands results in the same output:

8
9
10

Why there are no zeroes in front of 8 and 9? Is it because of bash version, or just environment configuration? Can I change it per session or permanently, so my server will put that zero in front of numbers smaller than 10?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Learner
  • 223
  • 1
  • 9

3 Answers3

3

It's probable that the bash on your CentOS 5.6 machine is version 3. Zero-filling of integers in brace expansions was introduced in version 4 of bash.

The solution would be to either update your installation of bash to a more recent version, or to use an alternative way of generating the zero-filled integers, like

printf '%02d\n' {8..10}

Alternatively, something along the lines of

for i in 0{1..9} {10..20}; do
    echo "$i"
done

will also work.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • Yes, you are right. Bash on my CentOS 7.2 is 4.x version, and bash on that server which I mentioned is 3.x version. I am marking this as a solution since I mostly wanted to learn why is the difference happening. Thanks! – Learner Apr 07 '17 at 07:36
2

Instead of using bash, you could use zsh where that syntax comes from.

zsh has been supporting {08..10} since 1995 (introduced in 2.6-beta4), so will be available in the zsh of any version of CentOS (first release of CentOS in 2004).

You did actually use zsh syntax in your example as in bash you need echo "${i}" instead of echo ${i} for the expansion of $i not to be subject to word splitting (and so dependant on $IFS).

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
1

alternative, you can use seq command

$ seq -f "%02g" 8 10
08
09
10
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
Kamaraj
  • 4,295
  • 1
  • 12
  • 18