How can I run the following command with curly braces in bash?
Well, if you use a variable on the command line like that, it will be split to words, but that happens after syntactical items like {
(or if
) are parsed. So, if you want that, you'll have to use eval
CMD="{ echo blah ; echo bleh; } > output"
eval "$CMD"
# output contains "blah" and "bleh"
Though note eval
will run everything in the variable in the current shell, including assignments to variables (changing IFS
may have funny effects for the rest of the script, say). You could run it in a separate shell with bash -c "$CMD"
to mitigate at least the variable assignment issue.
Also note that the backticks are used to capture the output of a command, and use it on the command line, so this would run the output of $CMD
also as a command:
$ CMD="echo foo"
$ `$CMD`
-bash: foo: command not found
If you're redirecting the output to a file, it won't matter, but you most likely also don't need it.
try
function exec_cmd() {
printf "+ %s \n\n" "$1"
bash -c "$1" || exit 1
}
exec_cmd '{ head -n1 inputFile.new && egrep -i "X|Y" inputFile.new; } > outputFile.new'
Then you don't have to worry about escaping the strings.
Note about bash -c <command>
usage, the next argument after <command>
is used as $0
(the script's "name" from inside the script), and subsequent arguments become the positional parameters ($1, $2, etc.).