Solved Why does this command have two very different results?

I was writing a script for /bin/sh to be used on a 10.3 box when I noticed a strange output for mkdir(1). I simply wanted mkdir(1) to make two folders.

FreeBSD 10.3 in sh(1)
Code:
$  mkdir -p -v /tmp/{test1,test2}
/tmp/{test1,test2}

It instead makes a folder called '{test1,test2}', which is what I don't want.

Running this on Manjaro Linux sh(1)
Code:
sh-4.3$ mkdir -p -v /tmp/{test1,test2}
mkdir: created directory '/tmp/test1'
mkdir: created directory '/tmp/test2'

I always though sh(1) code was supposed to be portable but what am I missing here?
 
To add to tobik's post, brace expansion is a bash(1) feature and not supported by sh(1) IIRC.

To expand on that, from the bash(1) man page:

Code:
       [...]
     
       Brace expansion introduces a  slight  incompatibility  with  historical
       versions of sh. sh does not treat opening or closing braces specially
       when they appear    as part    of a word, and preserves them in the  output.
       Bash  removes braces from  words as a consequence of brace expansion.
       For example, a word entered to sh as file{1,2} appears  identically  in
       the  output.  The same word is output as    file1 file2 after expansion by
       bash. If strict compatibility with sh is desired, start bash with the
       +B option or disable brace expansion with the +B option to the set com-
       mand (see SHELL BUILTIN COMMANDS below).

       [...]

Depending on the context of the script, the following simple command should work almost everywhere as is: mkdir -p -v file1 file2
 
The sh(1) shell in FreeBSD is a derivative of the Almquist Shell and lack of certain extensions is explained by its history and POLA considerations:

https://en.wikipedia.org/wiki/Almquist_shell

Unfortunately there isn't a well defined subset of shell features that one could rely on and produce a script that would run on every single system that has a Bourne -like shell. For example you can't even trust that `command` works with every shell called /bin/sh.
 
Back
Top