Capturing system command output as a string

Posted by dreeves on Stack Overflow See other posts from Stack Overflow or by dreeves
Published on 2008-10-25T17:44:53Z Indexed on 2010/03/23 20:53 UTC
Read the original article Hit count: 164

Perl and PHP do this with backticks. For example:

$output = `ls`;

This code returns a directory listing into the variable $output. A similar function, system("ls"), returns the operating system return code for the given command. I'm talking about a variant that returns whatever the command prints to stdout. (There are better ways to get the list of files in a directory; the example code is an example of this concept.)

How do other languages do this? Is there a canonical name for this function? (I'm going with "backtick"; though maybe I could coin "syslurp".)

© Stack Overflow or respective owner

Related posts about language-agnostic

Related posts about capture-output