Perl and PHP do this with backticks. For example,
$output = `ls`;
Returns a directory listing. A similar function, system(\"foo\")
Yet another way to do it in Perl (TIMTOWTDI)
$output = <<`END`;
ls
END
This is specially useful when embedding a relatively large shell script in a Perl program
Python:
import os
output = os.popen("foo").read()
In shell
OUTPUT=`ls`
or alternatively
OUTPUT=$(ls)
This second method is better because it allows nesting, but isn't supported by all shells, unlike the first method.
Yet another way (or 2!) in Perl....
open my $pipe, 'ps |';
my @output = < $pipe >;
say @output;
open can also be written like so...
open my $pipe, '-|', 'ps'
Clozure Common Lisp:
(with-output-to-string (stream)
(run-program "ls" '("-l") :output stream))
LispWorks
(with-output-to-string (*standard-output*)
(sys:call-system-showing-output "ls -l" :prefix "" :show-cmd nil))
Mathematica:
output = Import["!foo", "Text"];