Embed shell commands with interpolated Haskell variables, and capture output.
- sh :: QuasiQuoter
- shc :: QuasiQuoter
- readShell :: String -> IO String
- readShellWithCode :: String -> IO (ExitCode, String)
- showNonString :: (Typeable a, Show a) => a -> String
Quasiquoters
Execute a shell command, capturing output.
This requires the QuasiQuotes
extension.
The expression [sh| ... |]
has type
.
Executing this IO action will invoke the quoted shell
command and produce its standard output as a IO
String
.
String
>>>
[sh| sha1sum /proc/uptime |]
"ebe14a88cf9be69d2192dcd7bec395e3f00ca7a4 /proc/uptime\n"
You can interpolate Haskell
variables using the
syntax String
$x
. Special characters are escaped, so that the
program invoked by the shell will see each interpolated
variable as a single argument.
>>>
let x = "foo bar" in [sh| cat $x |]
cat: foo bar: No such file or directory *** Exception: ExitFailure 1
You can also write ${x}
to separate the variable name from
adjacent characters.
>>>
let x = "b" in [sh| echo a${x}c |]
"abc\n"
Be careful: the automatic escaping means that [sh| cat '$x'
|]
is less safe than [sh| cat $x |]
, though it will
work "by accident" in common cases.
To interpolate without escaping special characters, use
the syntax $+x
.
>>>
let x = "foo bar" in [sh| cat $+x |]
cat: foo: No such file or directory cat: bar: No such file or directory *** Exception: ExitFailure 1
You can pass a literal $
to the shell as \$
, or a
literal \
as \\
.
As demonstrated above, a non-zero exit code from the subprocess will raise an exception in your Haskell program.
Variables of type other than
are interpolated via
String
.
show
>>>
let x = Just (2 + 2) in [sh| touch $x; ls -l J* |]
"-rw-r--r-- 1 keegan keegan 0 Oct 7 23:28 Just 4\n"
The interpolated variable's type must be an instance of
and of Show
.
Typeable
Helper functions
These functions are used in the implementation of
, and may be useful on their own.
sh
readShell :: String -> IO StringSource
Execute a shell command, capturing output.
Used in the implementation of
.
sh