Run retrieving stdout without using RunProcess

If you prepend bash -c to your RunProcess command, you can pass ls -ltr | tail -1 as a single argument (at least with v12 on Windows 7 with cygwin or Windows 10 with the subsystem for linux).

RunProcess[{"bash", "-c", "ls -ltr | tail -1"}, "StandardOutput"]

One way is to write away the standard output to a file using the shell redirect "> file" to create a temporary file and read from there.

run[command_String]:=Module[{out},
 Run[command<>" > tmpstout.txt"];
 out=Import["tmpstout.txt","Text"];
 DeleteFile["tmpstout.txt"]; out]

Works for this for example.


Ironically the temporary file will first be created so for the command above it will be the file returned. Which is technically correct, (the best kind of correct) but might not be as intended.


This solutions is only a sketch and you have to be careful with the location of the temporay file (so that it is in the directory you are importing from). Anyway, don't use it since the accepted solution is much better.