Julia Running External Programs
Julia borrows the backtick syntax from Shell, Perl, and Ruby to run external commands. Commands use backticks ... to surround them:
Example
julia
mycommand =
`
echo hello
`
`
echo hello
`
julia
typeof
(
mycommand
)
Cmd
Note:Unlike Shell, the backticks themselves do not execute the command immediately, but instead create a
Cmdobject. When Julia runs a command it does not go through Shell but directly fork/exec a child process, so there are no Shell features such as wildcard expansion or pipes, and no quote escaping issues either.
Running commands: run
The run() function executes a command and prints the output directly to the terminal; a failed command throws ProcessFailedException:
Example
julia
run
(
`
echo hello
`
)
;
hello
An exception is thrown when a command fails
julia
run
(
`
false
`
)
ERROR: failed process: Process
(
`
false
`
, ProcessExited
(
1
)
)
[
1
]
Capturing command output
read() can be used to capture a command's output, and readchomp() also removes the trailing newline:
Example
julia
read
(
`
echo hello
`
,
String
)
"hello\n"
julia
readchomp
(
`
echo hello
`
)
"hello"
Practical use: getting the number of files
julia
parse
(
Int
, readchomp
(
`
ls
|
wc -l
`
)
)
Interpolation
Backtick commands also support $ interpolation; Julia automatically handles argument quoting and escaping, so there is no need to worry about special characters:
Example
julia
file =
"/Volumes/External HD/data.csv"
;
Paths with spaces are also safe; Julia automatically treats them as a single argument
julia
`
sort
$file
`
`
sort
'/Volumes/External HD/data.csv'
`
An interpolated array is expanded into multiple arguments
julia
files =
[
"/etc/passwd"
,
"/data.csv"
]
;
julia
`
grep foo $files
`
`
grep foo /etc/passwd /data.
csv
`
Pipelines: pipeline
Shell's |, &, > need to be escaped inside backticks; to construct a pipeline use the pipeline() function:
Example
Pipe the output of echo into sort
julia
run
(
pipeline
(
`
echo hello
`
,
`
sort
`
)
)
;
hello
A more complex example: counting the number of files in the current directory:
Example
julia
readchomp
(
pipeline
(
`
ls
`
,
`
wc -l
`
)
)
"12"
Redirect output to a file
julia
run
(
pipeline
(
`
echo runoob
`
,
"out.txt"
)
)
Process
(
`
echo runoob
`
, ProcessExited
(
0
)
)
Two commands executed in parallel with their output combined
julia
run
(
pipeline
(
`
echo world
`
&
`
echo hello
`
,
`
sort
`
)
)
;
hello
world
Note:On Windows, echo, dir, etc. are built-in commands of cmd.exe rather than external programs, so they need to be written as
cmd /C echo helloto run. Julia officially recommends implementing the same functionality in pure Julia first.
Options of the Cmd object
The Cmd constructor can be used to specify options such as the working directory and environment variables for a command:
Example
The dir option specifies the working directory
julia
run
(
Cmd
(
`
pwd
`
, dir=
"/"
)
)
;
/
The env option sets environment variables
julia
run
(
Cmd
(
`
sh -c
"echo foo $HOWLONG"
`
, env=
(
"HOWLONG"
=
"ever!"
,
)
)
)
;
foo ever
!
ignorestatus ignores the command's exit code (no exception thrown on failure)
julia
run
(
Cmd
(
Cmd
(
[
"false"
]
)
,
ignorestatus
=
true
)
)
Process
(
`
false
`
, ProcessExited
(
1
)
)
setenv and addenv provide another way to replace or append environment variables:
Example
julia
run
(
addenv
(
`
sh -c
"echo foo $HOWLONG"
`
,
"HOWLONG"
=
"ever!"
)
)
;
foo ever
!
The trade-off between Julia and Shell
Many things Shell can do can be done by Julia itself; the official recommendation is to prefer pure Julia:
| Shell notation | Julia alternative notation |
|---|---|
ls *.txt(wildcards) |
filter(endswith(".txt"), readdir()) or the Glob.jl package |
cat a.txt |
read("a.txt", String) |
rm file.txt |
rm("file.txt") |
mkdir dir |
mkdir("dir") |
command1 \| command2 |
Read the former's output and pass it to the latter, or pipeline |
Reasons to avoid going through Shell by default: slow process startup, fragile escaping of special characters, poor error handling, and poor cross-platform support. Julia executes child processes directly, bypassing all these pitfalls.