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?

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.

  • 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 7 '17 at 7: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).

  • Thanks, that's good to know. I wasn't aware that it comes from zsh. – Learner Apr 7 '17 at 10:44
  • @Learner, most bash features come from ksh, a few from csh/tcsh like history expansion, and a few from zsh. It has very few of its own. – Stéphane Chazelas Apr 7 '17 at 11:23
1

alternative, you can use seq command

$ seq -f "%02g" 8 10
08
09
10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.