I have trouble using a command line executable. I need to pass a potentially empty string to a program from bash, but the string is stored in a bash variable.
The (minimal working example of the C++) program is:
// Compile with g++ main.cpp -o soexec
#include <iostream>
int main(int argc, char** argv) {
// shows what the program arguments are
for(size_t ind = 1 ; ind < argc; ++ind)
std::cout << " argv[" << ind << "]={" << argv[ind] << "}" << std::endl;
// real application code should go here
return EXIT_SUCCESS;
}
When I use the program without variables, I got the following, as expected:
$ ./soexec --paramName ""
argv[1]={--paramName}
argv[2]={} # empty string, as wanted
$ EMPTY_VARIABLE=
$ ./soexec --paramName "${EMPTY_VARIABLE}"
argv[1]={--paramName}
argv[2]={} # empty string, as wanted
However, if the command lines arguments are stored in a variable, I get an issue
$ VARIABLE_NAME=
$ ARGS="--paramName $VARIABLE_NAME"
$ ./soexec $ARGS
argv[1]={--paramName}
# no argv[2]!
I also tried
$ VARIABLE_NAME=\"\"
$ ARGS="--paramName $VARIABLE_NAME"
$ ./soexec $ARGS
argv[1]={--paramName}
argv[2]={""} # this time the string is no longer empty, it contains the quote...
How can in pass a potentially empty string in bash to a command-line program ?
Note: this question has been marked as a duplicate of When should I wrap quotes around a shell variable?. However, it not only about quote in shell variable, it is about variable stored in another variable.
PS: for more context, my initial (and now resolved) problem was: What's the best way to pass an empty string as argument to boost::program_options?