denysdovhan/bash-handbook

{folder1,folder2,folder3} doesn't work in sh

crapthings opened this issue · 7 comments

thanks for this great handbook.

i have a question that is
i want to make many dirs in a folder
but what i get is just folder with a longname like this
{client,server,collections,lib,public}

#!/bin/bash
# learn shell script

$PROJECTNAME=$1
mkdir -p $PROJECTNAME/{client,server,collections,lib,public}

That's OK, because of sh doesn't understand these brace expansions. Just run your script using bash:

bash ./your_script

or add a shebang like below:

#!/usr/bin/env bash

mkdir -p "$1"/{client,server,collections,lib,public}

awesome, what is different between these ?

#!/bin/bash

#!/usr/bin/env bash

The second way is more universal. Learn more here: http://stackoverflow.com/a/16365367/5508862

And thank you for your question! 😺

While I don't disagree with the shebang advice above, I don't think that's the cause of this problem.

The original code contains a minor typo (and common mistake): $PROJECTNAME=$1 should be PROJECTNAME=$1; the $ sigil is used when expanding variables (on the right side of the =) but not on the left side.

On OSX 10.10.3 with bash 3.2.57, I see the same behavior whether my shebang is #!/bin/bash, #!/usr/bin/env bash, #!/bin/sh, or #!/usr/bin/env sh.

I also confirmed that original Bourne shell (compiled from https://github.com/grml/heirloom-sh) does not support {} expansion (as @denysdovhan mentioned above).

NOTE: If PROJECTNAME could contain whitespace, take care to expand it within quotes (as @denysdovhan does with "$1") to avoid undesired word-splitting.

@sumbach hmmm, I didn't know about it. It works well for me, but maybe it's because I use Linux.

oh i mean

PROJECTNAME=$1
$PROJECTNAME=$1

okay that's my typo error, after change to

#!/usr/bin/env bash

it works on osx