# Executing system commands "backtick-like" way in Julia?

**URL:** https://discourse.julialang.org/t/executing-system-commands-backtick-like-way-in-julia/114890
**Category:** New to Julia
**Tags:** question
**Created:** [May 29, 2024, 5:33am UTC](https://discourse.julialang.org/t/executing-system-commands-backtick-like-way-in-julia/114890 "2024-05-29T05:33:49Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![oOosys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ooosys/32/209566_2.png) [@oOosys](https://discourse.julialang.org/u/oOosys)
#### Post date: [May 29, 2024, 5:33am UTC](https://discourse.julialang.org/t/executing-system-commands-backtick-like-way-in-julia/114890/1 "2024-05-29T05:33:49Z")

</div>

In Ruby I can put a shell command in backtick quotes and will get the printed output and the exit code as response. How to accomplish the same in Julia with as less prefix and postfix code wrapping the string of the shell command as possible?

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [May 29, 2024, 5:37am UTC](https://discourse.julialang.org/t/executing-system-commands-backtick-like-way-in-julia/114890/2 "2024-05-29T05:37:16Z")

</div>

Do you need more than `run(`somebin some args`)`?

---

<div class="post-metadata">

### Author: ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)
#### Post date: [May 29, 2024, 6:14am UTC](https://discourse.julialang.org/t/executing-system-commands-backtick-like-way-in-julia/114890/3 "2024-05-29T06:14:53Z")

</div>

See [Running External Programs · The Julia Language](https://docs.julialang.org/en/v1/manual/running-external-programs/#Running-External-Programs)

---

<div class="post-metadata">

### Author: ![oOosys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ooosys/32/209566_2.png) [@oOosys](https://discourse.julialang.org/u/oOosys)
#### Post date: [May 29, 2024, 8:41pm UTC](https://discourse.julialang.org/t/executing-system-commands-backtick-like-way-in-julia/114890/4 "2024-05-29T20:41:02Z")

</div>

I would like to have it as unobtrusive to a surrounding text flow as possible. `run(` as prefix and `)` as postfix does not align with the surrounding text flow as good as backticks which do not noticeably disturb the text flow … so for example using some Unicode characters like for example » as prefix and « as postfix would be much better and maybe could be replaced by backtick-like other Unicode ones. Is this possible in Julia? Or best to overwrite the backtick behavior with own custom function taking the backtick content as paramater?

---

<div class="post-metadata">

### Author: ![oOosys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ooosys/32/209566_2.png) [@oOosys](https://discourse.julialang.org/u/oOosys)
#### Post date: [May 29, 2024, 8:42pm UTC](https://discourse.julialang.org/t/executing-system-commands-backtick-like-way-in-julia/114890/5 "2024-05-29T20:42:53Z")

</div>

Sorry for the late reply, but I was blocked by the chat system - written too much messages within one day …

---

<div class="post-metadata">

### Author: ![mnemnion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mnemnion/32/206596_2.png) [@mnemnion](https://discourse.julialang.org/u/mnemnion)
#### Post date: [May 29, 2024, 8:56pm UTC](https://discourse.julialang.org/t/executing-system-commands-backtick-like-way-in-julia/114890/6 "2024-05-29T20:56:24Z")

</div>

> [@oOosys](#):
>
> I would like to have it as unobtrusive to a surrounding text flow as possible. `run(` as prefix and `)` as postfix does not align with the surrounding text flow as good as backticks which do not noticeably disturb the text flow

How about:

```julia
output = `my shell command` |> run

```

---

<div class="post-metadata">

### Author: ![mnemnion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mnemnion/32/206596_2.png) [@mnemnion](https://discourse.julialang.org/u/mnemnion)
#### Post date: [May 29, 2024, 8:58pm UTC](https://discourse.julialang.org/t/executing-system-commands-backtick-like-way-in-julia/114890/7 "2024-05-29T20:58:53Z")

</div>

Or this:

```julia
julia> macro r_cmd(str)
           return `$str` |> run
       end
@r_cmd (macro with 1 method)

julia> r`ls`

```

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [May 29, 2024, 9:07pm UTC](https://discourse.julialang.org/t/executing-system-commands-backtick-like-way-in-julia/114890/8 "2024-05-29T21:07:57Z")

</div>

> [@mnemnion](#):
>
> ```julia
> macro r_cmd(str)
> return `$str` |> run
> end
> 
> ```

This doesn’t quite work with arguments (like `r`ls -alh` `), but we can re-use Base’s argument parsing to do so:

```julia
macro r_cmd(str)
    return :(run(@cmd($(str))))
end

```

---

<div class="post-metadata">

### Author: ![oOosys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ooosys/32/209566_2.png) [@oOosys](https://discourse.julialang.org/u/oOosys)
#### Post date: [May 29, 2024, 9:12pm UTC](https://discourse.julialang.org/t/executing-system-commands-backtick-like-way-in-julia/114890/9 "2024-05-29T21:12:35Z")

</div>

> [@ericphanson](#):
>
> ```julia
> macro r_cmd(str)
> return :(run(@cmd($(str))))
> end
> 
> ```

r`ls '*.svg'`  
ls: cannot access ‘\*.svg’: No such file or directory  
ERROR: failed process: Process(`ls '*.svg'`, ProcessExited(2)) [2]

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 29, 2024, 9:14pm UTC](https://discourse.julialang.org/t/executing-system-commands-backtick-like-way-in-julia/114890/10 "2024-05-29T21:14:28Z")

</div>

> [@oOosys](#):
>
> I would like to have it as unobtrusive to a surrounding text flow as possible. `run(` as prefix and `)` as postfix does not align with the surrounding text flow as good as backticks which do not noticeably disturb the text flow

You can put the command on a different line, with whatever indentation you want, using triple backticks, e.g.:

````julia
result = readchomp(```
    ls
```)

````

(I would generally recommend trying too hard to make Julia code look like Ruby or some other language. Get used to differences in surface syntax between languages, try to write idiomatic code that other Julia programmers can easily read, and focus more on what the code _does_.)

Note also that most shell commands have native Julia equivalents, and it is better (more flexible, more robust, higher performance) in the long run to learn to use native Julia code for typical shell operations like `ls` or `grep` or `cut`.

---

<div class="post-metadata">

### Author: ![mnemnion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mnemnion/32/206596_2.png) [@mnemnion](https://discourse.julialang.org/u/mnemnion)
#### Post date: [May 29, 2024, 9:23pm UTC](https://discourse.julialang.org/t/executing-system-commands-backtick-like-way-in-julia/114890/11 "2024-05-29T21:23:07Z")

</div>

> [@ericphanson](#):
>
> This doesn’t quite work with arguments (like `r`ls -alh` `), but we can re-use Base’s argument parsing to do so:

Good catch!

OP also wanted the results in a string, and to include the return code.

Therefore:

```julia
julia> macro r_cmd(str)
           quote
               io = IOBuffer()
               try
                   run(@cmd($(str)), stdin, io)
               catch e
                   return String(take!(io)), e.procs[1].exitcode
               end
               return String(take!(io)), 0
           end
       end
@r_cmd (macro with 1 method)

julia> r`false`
("", 1)

julia> r`true`
("", 0)
```

---

<div class="post-metadata">

### Author: ![oOosys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ooosys/32/209566_2.png) [@oOosys](https://discourse.julialang.org/u/oOosys)
#### Post date: [May 29, 2024, 9:24pm UTC](https://discourse.julialang.org/t/executing-system-commands-backtick-like-way-in-julia/114890/12 "2024-05-29T21:24:02Z")

</div>

What I am after is making as it easy as possible to write a transpiler for conversion from a like English sentences looking text written following guidelines of an oOo way of approaching programming I am busy with… Best I don’t need a transpiller at all because the target programming language allows to make another kind of programming language out of it giving the user the freedom of choice of anything. Badly languages which allowing this provide as good as no “batteries included” so it would mean to rewrite anything from scratch - what is not practicable - this leads to usage of higher level languages best suited to fit into the core idea. The question is how good is Julia suited for this purpose? Ruby is for example much better suited compared to Python. The oOo system requires usage of one-based indexing - a strong argument towards using Julia … Currently the shell programming language is the most flexible allowing any Unicode in “symbol names” being executable files … Ruby is two backticks away from shell programming … but with zero based indexing like shell …

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [May 29, 2024, 9:26pm UTC](https://discourse.julialang.org/t/executing-system-commands-backtick-like-way-in-julia/114890/13 "2024-05-29T21:26:45Z")

</div>

That also doesn’t work with Julia’s regular commands (i.e. `run(`rls '*.svg'`)`) since it needs a shell to do globbing. In either case you can do `sh -c ...` or such.

---

<div class="post-metadata">

### Author: ![oOosys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ooosys/32/209566_2.png) [@oOosys](https://discourse.julialang.org/u/oOosys)
#### Post date: [May 29, 2024, 9:35pm UTC](https://discourse.julialang.org/t/executing-system-commands-backtick-like-way-in-julia/114890/14 "2024-05-29T21:35:39Z")

</div>

This looks promising … now it needs to be able to take implicit the shell which will be running the command …

---

<div class="post-metadata">

### Author: ![oOosys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ooosys/32/209566_2.png) [@oOosys](https://discourse.julialang.org/u/oOosys)
#### Post date: [May 29, 2024, 9:40pm UTC](https://discourse.julialang.org/t/executing-system-commands-backtick-like-way-in-julia/114890/15 "2024-05-29T21:40:01Z")

</div>

It seems that macro programming is the way to arrive at a useful result … how does it work? Strange syntax … is the string `str` passed to `r_cmd(str)` the string within the backticks? and the `cmd` the hint which command the macro takes it parameter from? In this case it is a question of coding the macro to provide any desired functionality - or are macroes limited in what they can do?

---

<div class="post-metadata">

### Author: ![oOosys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ooosys/32/209566_2.png) [@oOosys](https://discourse.julialang.org/u/oOosys)
#### Post date: [May 29, 2024, 9:54pm UTC](https://discourse.julialang.org/t/executing-system-commands-backtick-like-way-in-julia/114890/16 "2024-05-29T21:54:36Z")

</div>

```julia
julia> `bash -c 'ls "*.svg"'` |> run
ls: cannot access '*.svg': No such file or directory
ERROR: failed process: Process(`bash -c 'ls "*.svg"'`, ProcessExited(2)) [2]

```

```julia
julia> r`bash -c 'ls *.svg'`
ls: cannot access '*.svg': No such file or directory
("", 2)

```

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [May 29, 2024, 10:37pm UTC](https://discourse.julialang.org/t/executing-system-commands-backtick-like-way-in-julia/114890/17 "2024-05-29T22:37:57Z")

</div>

````julia-repl
julia> ```
bash -c "ls *.svg"
``` |> run

````

> [@oOosys](#):
>
> It seems that macro programming is the way to arrive at a useful result … how does it work? Strange syntax … is the string `str` passed to `r_cmd(str)` the string within the backticks? and the `cmd` the hint which command the macro takes it parameter from? In this case it is a question of coding the macro to provide any desired functionality - or are macroes limited in what they can do?

Macros are basically functions that take an expression and output a modified expression, which is then evaluated after the function returns.

[https://docs.julialang.org/en/v1/manual/metaprogramming/](https://docs.julialang.org/en/v1/manual/metaprogramming/)

---

<div class="post-metadata">

### Author: ![oOosys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ooosys/32/209566_2.png) [@oOosys](https://discourse.julialang.org/u/oOosys)
#### Post date: [May 29, 2024, 11:44pm UTC](https://discourse.julialang.org/t/executing-system-commands-backtick-like-way-in-julia/114890/18 "2024-05-29T23:44:37Z")

</div>

The triple backtick code example fails to work `ls: cannot access '*.svg': No such file or directory`

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [May 30, 2024, 1:31am UTC](https://discourse.julialang.org/t/executing-system-commands-backtick-like-way-in-julia/114890/19 "2024-05-30T01:31:41Z")

</div>

I can only replicate that error if I do the the following.

````julia
julia> ```
bash -c "ls '*.svg'"
``` |> run

````

Do not add extra single quotes around `*.svg`.
