A way to do this without read
ing into the shell, in a traditional tools pipeline approach:
echo "$one" |
tr ',' '\n' |
sed "s/^/mkdir 'type-/; s/$/'/" |
sh -x
Your original attempt was very close. To make it work, you can use the shell eval
command:
eval mkdir type-{$one}
or
echo mkdir type{"$one"} | bash
In either case, the effect causes bash
to re-evaluate the line.
I personally would not recommend this approach for these reasons:
eval
can be a security risk and is little used, maintainers will have to do a double-take.
- Brace Expansion is a
bash
-type shell extension and while I love bash, I write all shell scripts to run with the POSIX /bin/sh
.
- These will not handle unusual characters in filenames, such as spaces.
The eval
causes the shell to re-evaluate the string after the variable substition has been performed. To gain more understanding on these topics, see "Brace Expansion" and also the eval
command, both on the bash
man page.