11

Do I have to do any configuration to start brace expansion? When I run mkdir {1..10}, it just creates a dir naming {1..10}.

muru
  • 72,889
user43312
  • 961

3 Answers3

12

While brace expansion like {1,2} originates in csh in the late 70s, and found its way to Bourne-like shells in bash/zsh/pdksh in the late 80s, early 90s, the {n1..n2} variant came later first in zsh in 1995 (2.6-beta4).

bash copied it in 2004 (3.0) and ksh93 in 2005 (ksh93r).

Probably the shell you're trying this in is neither of those or is an older version of bash and ksh93.

  • We should as about what kind of shebank shell he used. /me suspects it's #!/bin/sh... – polemon Sep 29 '13 at 08:25
  • 3
    @polemon, That would not necessarily help. For instance, on some systems /bin/sh is bash or ksh93. – Stéphane Chazelas Sep 29 '13 at 08:34
  • 2
    I actually think it would: depending on the invocation, shells invoked as /bin/sh act as the historic sh (it's what the bash man page says anyway). Haven't checked ksh's man page, but I'm sure it'll behave accordingly. – polemon Sep 29 '13 at 08:44
  • @Stephane Chazelas. Yes I am using the very old RH9. Could you tell me how to check bash version? – user43312 Sep 29 '13 at 10:28
  • @polemon, you really confused me. Doesn't the shebang line appear inside a script file? But I ran the mkdir just on command line. – user43312 Sep 29 '13 at 10:33
  • @user43312 sorry, I didn't mean to. It was unclear, whether you're referring to something used inside a script or directly from the shell. Errors like this usually creep up in shell scripting, due to the different shells being used as interactive shell and as scripting interpreter. – polemon Sep 29 '13 at 10:55
  • 1
    @user43312: why are you still using RH9? It has been unmaintained for many many years. AS Stephane wrote it is a feature of recent bash versions. So it's very unlikely that your bash version supports it. You can check the version using bash --version. And just to be sure check if you are running a bash shell by checking the $SHELL variable or simply running ps. To use this feature you might try to start a zsh shell (zsh) first. Anothher workaround would be to use something like "mkdir $(seq 1 10)". – Bram Sep 29 '13 at 11:10
  • @polemon You've misunderstood what bash does when invoked as sh -- it runs in POSIX mode, it doesn't mimic another shell. http://sprunge.us/dbYL – Chris Down Sep 29 '13 at 13:03
  • @ChrisDown It kinda clearly says that in the man page: "If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well." http://linux.die.net/man/1/bash – polemon Sep 29 '13 at 14:14
  • 1
    @polemon The startup behaviour of another shell (ie. which files it sources upon startup), not mimicry of another shell. – Chris Down Sep 29 '13 at 14:36
  • @ChrisDown: "while conforming to the POSIX standard as well", which changes some things in the way bash behaves – Olivier Dulac Oct 02 '13 at 17:10
  • @OlivierDulac Right, but that's not mimicry of another shell, it's just compatibility fixups. The changes are fairly minor. – Chris Down Oct 03 '13 at 01:00
4

{x..y} Range brace expansion is implemented in bash 3.0-alpha. To help us and yourself, please show your echo "$BASH_VERSION" output.

Then the answer: If mkdir {1..10} creates a dir with the name {1..10} then you are using a bash version prior bash 3.0-alpha. In that case you can use a for loop construction as:

for ((i=1;i<=10; i++)); do mkdir "$i"; done

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
1

Check your Bash version:

$ bash -version
GNU bash, version 4.1.7(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

Also check that mkdir hasn't been aliases in some strange way:

$ alias |grep mkdir
alias md='mkdir'
slm
  • 369,824