run
run(command)
Run a command object, constructed with backticks. Throws an error if anything goes wrong, including the process exiting with a non-zero status.
Examples
julia> run(`echo hello`)
hello
julia> run(`echo "hello"`)
hello
julia> run(`echo (hello)`)
(hello)
julia> run(`ls`)
file1.txt
file2.txt
file3.txt
Here are some common examples of its use:
-
Run a command and display output:
julia> run(`echo "Hello, Julia!"`) Hello, Julia!
This example runs the
echo
command with the argument"Hello, Julia!"
and displays the output. -
Execute a system command:
julia> run(`git status`) On branch master Your branch is up to date with 'origin/master'. nothing to commit, working tree clean
In this example, the
git status
command is executed, and the output is displayed. - Throw an error if command fails:
julia> run(`false`) ERROR: failed process: Process(`false`, ProcessExited(1)) [1]
When a command fails, such as running the
false
command, an error is thrown.
Common mistake example:
julia> run("echo 'Hello, Julia!'")
ERROR: MethodError: no method matching run(::String)
In this example, the run
function is incorrectly used with a string argument instead of a command object constructed with backticks. It's important to construct the command with backticks (`) to create a command object before passing it to
run`.
See Also
addprocs, atexit, cd, clipboard, EnvHash, exit, getpid, peakflops, ProcessExitedException, process_exited, process_running, procs, quit, readandwrite, redirect_stdout, rmprocs, run, setenv, spawn, withenv,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.