Skip to content

Commit

Permalink
FAQ entry on shell metacharacters in run (#43633)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj authored Jan 5, 2022
1 parent f958438 commit b9432a8
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions doc/src/manual/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,25 @@ parsing the file once it reaches to the `exec` statement.
```
instead. Note that with this strategy [`PROGRAM_FILE`](@ref) will not be set.

### Why doesn't `run` support `*` or pipes for scripting external programs?

Julia's [`run`](@ref) function launches external programs *directly*, without
invoking an [operating-system shell](https://en.wikipedia.org/wiki/Shell_(computing))
(unlike the `system("...")` function in other languages like Python, R, or C).
That means that `run` does not perform wildcard expansion of `*` (["globbing"](https://en.wikipedia.org/wiki/Glob_(programming))),
nor does it interpret [shell pipelines](https://en.wikipedia.org/wiki/Pipeline_(Unix)) like `|` or `>`.

You can still do globbing and pipelines using Julia features, however. For example, the built-in
[`pipeline`](@ref) function allows you to chain external programs and files, similar to shell pipes, and
the [Glob.jl package](https://github.com/vtjnash/Glob.jl) implements POSIX-compatible globbing.

You can, of course, run programs through the shell by explicitly passing a shell and a command string to `run`,
e.g. ```run(`sh -c "ls > files.txt"`)``` to use the Unix [Bourne shell](https://en.wikipedia.org/wiki/Bourne_shell),
but you should generally prefer pure-Julia scripting like ```run(pipeline(`ls`, "files.txt"))```.
The reason why we avoid the shell by default is that [shelling out sucks](https://julialang.org/blog/2012/03/shelling-out-sucks/):
launching processes via the shell is slow, fragile to quoting of special characters, has poor error handling, and is
problematic for portability. (The Python developers came to a [similar conclusion](https://www.python.org/dev/peps/pep-0324/#motivation).)

## Functions

### I passed an argument `x` to a function, modified it inside that function, but on the outside, the variable `x` is still unchanged. Why?
Expand Down

0 comments on commit b9432a8

Please sign in to comment.