[?] Pass string to << for executing external command?

Hi there,

I’m having trouble passing a string containing a valid command using <<, e.g.:

In[170]:= <<"!echo hello world"                                                                            

Out[170]= hello world

In[171]:= myCmdStr = "\"!echo hello world\""                                                               

Out[171]= "!echo hello world"

In[172]:= <<myCmdStr                                                                                       

Get::noopen: Cannot open myCmdStr.

Out[172]= $Failed

Is there a way to make this work? I would like to assemble a string with configurable parameters as input to an external command, execute and then get the return value. Perhaps there is a more elegant solution?

Thanks in advance!

Use RunProcess:

RunProcess["echo", All, "hello world"]

great, thanks!

p.s. I believe the exact syntax is (for the “all” case):

RunProcess[{"echo","hello world"}]

and

RunProcess[{"echo","hello world"}, "StandardOutput"] 

for just the output itself.

Thanks again! :slight_smile:

That was just an example from the documentation. You can also do it like this:

RunProcess[$SystemShell, All, "echo example line 1
 echo example line 2
 exit
 "]

giving:

<|"ExitCode" -> 0, "StandardOutput" -> "example line 1
  example line 2
  ", "StandardError" -> ""|>

Ah, got it! (misunderstood the intent of the one example with “All” above, which only returns the exit code :slight_smile:

Thanks again!