2

I'm trying to get an expansion command to work with runtime.exec, but the braces are being interpreted as literals rather than being expanded. Here's what I'm trying to do:

String command = "mkdir -p Foldername{1,2,3}/InnerFolder";
Runtime.getRuntime().exec( new String[] { "sh", "-c", command } );

Unfortunately, that gives me a single folder in my current directory named "Foldername{1,2,3}" instead of "Foldername1", "Foldername2", and "Foldername3". Does anyone know of a way to prevent the braces from being interpreted as literals?

David Young
  • 441
  • 1
  • 4
  • 13

1 Answers1

3

You're trying to use Bash wildcards. They are interpreted within the Bash shell. You are running mkdir directly, so there is no shell to interpret {}. You need to specify path to the shell

String command = "mkdir -p Foldername{1,2,3}/InnerFolder";
Runtime.getRuntime().exec( new String[] { "/bin/bash", "-c", command } );

Source.

Community
  • 1
  • 1
Mateusz
  • 3,038
  • 4
  • 27
  • 41
  • Oddly enough, it was specifying "bash" rather than just "sh" that solved it for me. Gotta love how AIX does everything just a little bit differently than the rest of the world... Regardless, thank you! – David Young May 10 '13 at 16:45
  • @David: It's not an AIX thing. The Bourne shell (`sh`) doesn't support brace expansions. – Dennis Williamson May 10 '13 at 17:27
  • I meant more than sh/bash are actually different on AIX. On every Linux system I've been on lately, sh and bash are symlinked (don't remember which way it goes) but the same binary gets executed in either case. – David Young May 10 '13 at 21:24