Alex Rivera | Logout

Making a system call that returns the stdout output as a string

Asked 2008-10-25T17:44:53.717
16

Perl and PHP do this with backticks. For example,

$output = `ls`;

Returns a directory listing. A similar function, system("foo"), returns the operating system return code for the given command foo. I'm talking about a variant that returns whatever foo prints to stdout.

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".)

Edit
Report

1 Answer

3

Perl:

$output = `foo`;

ADDED: This is really a multi-way tie. The above is also valid PHP, and Ruby, for example, uses the same backtick notation as well.

answered 2008-10-25T17:45:31.250

Your Answer