I am trying to run the following bash command from my C++ program:
diff <(cat /etc/passwd) <(ls -l /etc)
with the following C++ statement:
system("diff <(cat /etc/passwd) <(ls -l /etc)");
The command works fine when running it directly from the Linux shell but when running it from my program, I get:
sh: 1: Syntax error: "(" unexpected
That's referring to the (
I have tried escaping the (
with a \
, but that creates more issues:
system("diff <\\(cat /etc/passwd\\) <\\(ls -l /etc\\)");
sh: 1: cannot open (cat: No such file
All I want is to run the following from my C++ program:
diff <(cat /etc/passwd) <(ls -l /etc)
I can create a file and run it, but I leave that as a last option.
Example of command substitution using $() in Linux: Again, $() is a command substitution which means that it “reassigns the output of a command or even multiple commands; it literally plugs the command output into another context” (Source).
To exit from bash type exit and press ENTER . If your shell prompt is > you may have typed ' or " , to specify a string, as part of a shell command but have not typed another ' or " to close the string. To interrupt the current command press CTRL-C .
Set –e is used within the Bash to stop execution instantly as a query exits while having a non-zero status. This function is also used when you need to know the error location in the running code.
In many cases, bash scripts require argument values to provide input options to the script. You can handle command-line arguments in a bash script in two ways. One is by using argument variables, and another is by using the getopts function.
As mentioned system()
creates a new standard shell sh
and executes the commands. Since <()
is a bash specific feature it can't be interpreted by sh
.
You can circumvent this by calling bash
explicitly and use the -c
option:
system("bash -c \"diff <(cat /etc/passwd) <(ls -l /etc)\"");
or using a raw string literal:
system(R"cmd(bash -c "diff <(cat /etc/passwd) <(ls -l /etc)")cmd");
Here's the relevant part of the system(3)
call manual page:
The
system()
library function usesfork(2)
to create a child process that executes the shell command specified in command usingexecl(3)
as follows:execl("/bin/sh", "sh", "-c", command, (char *) 0);
system()
returns after the command has been completed.
The system(3)
call invokes /bin/sh
to process the command. If you want specifically use bash
features, you need to insert bash -c
in front of the command string, which will run bash
and tell it to process the remainder of the string.
system("bash -c \"diff <(cat /etc/passwd) <(ls -l /etc)\"");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With