Home > front end >  pass arguments to a command in bash
pass arguments to a command in bash

Time:10-05

I'm trying to pass arg to clang-format:

arg="-style=\"{BreakBeforeBraces: Attach}\""
clang-format -i $arg 'myfile.h'

but got the following error:

No such file or directory
Invalid value for -style

However, if I simply run the command like below:

clang-format -i -style="{BreakBeforeBraces: Attach}" 'myfile.h'

It works perfectly fine.

CodePudding user response:

You can simply create a function like this:

cfmt() {
   clang-format -i "$@"
}

Then use it as:

cfmt -style="{BreakBeforeBraces: Attach}" myfile.h

Other safe way is to store arguments in a shell array:

arg=('-i' '-style="{BreakBeforeBraces: Attach}"')

# use it as
clang-format "${arg[@]}" 'myfile.h'

CodePudding user response:

Shell removes the double quotes when you run the command directly, so no need to quote them in the variable value.

You need to doublequote the variable, though, to keep its content one word:

arg='-style={BreakBeforeBraces: Attach}'
clang-format -i "$arg" myfile.h
  • Related