# How to check if a program exists in a system using Julia?

**URL:** <https://discourse.julialang.org/t/how-to-check-if-a-program-exists-in-a-system-using-julia/19604>\
**Category:** General Usage\
**Created:** [January 14, 2019, 1:39am UTC](https://discourse.julialang.org/t/how-to-check-if-a-program-exists-in-a-system-using-julia/19604 "2019-01-14T01:39:39Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zranz](https://avatars.discourse-cdn.com/v4/letter/z/90ced4/32.png) [@Zranz](https://discourse.julialang.org/u/Zranz)\
**Post date:** [January 14, 2019, 1:39am UTC](https://discourse.julialang.org/t/how-to-check-if-a-program-exists-in-a-system-using-julia/19604/1 "2019-01-14T01:39:40Z")

</div>

I wanted to query if a program is installed in a Linux system. From `bash` I would do something as `type -p program` or similar, as explained [here](https://stackoverflow.com/questions/592620/how-to-check-if-a-program-exists-from-a-bash-script).

If I try something such as `read(`type -p program`)` within julia I get an error. `command -v program` or `hash program` also do not work. If I try `which program` I get an error if the program does not exist. Any ideas?

In the meantime I am querying if the file/program exists in a known location. Thanks!

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [January 14, 2019, 2:11am UTC](https://discourse.julialang.org/t/how-to-check-if-a-program-exists-in-a-system-using-julia/19604/2 "2019-01-14T02:11:24Z")

</div>

If speed is not a concern here, then you can always use:

```julia
julia> a = run(`type -p ls`)

```

which will throw an error if the process does not return with exit code 0.

---

<div class="post-metadata">

**Author:** ![Zranz](https://avatars.discourse-cdn.com/v4/letter/z/90ced4/32.png) [@Zranz](https://discourse.julialang.org/u/Zranz)\
**Post date:** [January 14, 2019, 2:31am UTC](https://discourse.julialang.org/t/how-to-check-if-a-program-exists-in-a-system-using-julia/19604/3 "2019-01-14T02:31:15Z")

</div>

I don’t think your suggestion works, I get an error. Secondly, `read` or `run` are very similar, only that `read` catches `stdout` and `run` returns `nothing` instead.

The problem I think is that `run` or `read` do not call the shell at all, and although many standard bash calls have been implemented (such as `tail`, `less`, `cat`, `ls` or others), `hash`, `type` or `command` haven’t. I do not know if they should, though.

An alternative could be

```julia
try run(`which nano`)
catch
end

```

Thanks.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [January 14, 2019, 2:44am UTC](https://discourse.julialang.org/t/how-to-check-if-a-program-exists-in-a-system-using-julia/19604/4 "2019-01-14T02:44:36Z")

</div>

Well, my suggestion is working fine here. I am using macOS, what error are you seeing?

---

<div class="post-metadata">

**Author:** ![Zranz](https://avatars.discourse-cdn.com/v4/letter/z/90ced4/32.png) [@Zranz](https://discourse.julialang.org/u/Zranz)\
**Post date:** [January 14, 2019, 2:46am UTC](https://discourse.julialang.org/t/how-to-check-if-a-program-exists-in-a-system-using-julia/19604/5 "2019-01-14T02:46:32Z")

</div>

I am using Ubuntu 18.04, julia v1.0.3.

```julia
julia> a = run(`type -p ls`)
ERROR: IOError: could not spawn `type -p ls`: no such file or directory (ENOENT)
Stacktrace:
 [1] _jl_spawn(::String, ::Array{String,1}, ::Cmd, ::Tuple{RawFD,RawFD,RawFD}) at ./process.jl:367
 [2] (::getfield(Base, Symbol("##493#494")){Cmd})(::Tuple{RawFD,RawFD,RawFD}) at ./process.jl:509
 [3] setup_stdio(::getfield(Base, Symbol("##493#494")){Cmd}, ::Tuple{RawFD,RawFD,RawFD}) at ./process.jl:490
 [4] #_spawn#492(::Nothing, ::Function, ::Cmd, ::Tuple{RawFD,RawFD,RawFD}) at ./process.jl:508
 [5] _spawn at ./process.jl:504 [inlined]
 [6] #run#503(::Bool, ::Function, ::Cmd) at ./process.jl:662
 [7] run(::Cmd) at ./process.jl:661
 [8] top-level scope at none:0

```

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [January 14, 2019, 2:49am UTC](https://discourse.julialang.org/t/how-to-check-if-a-program-exists-in-a-system-using-julia/19604/6 "2019-01-14T02:49:15Z")

</div>

Did you try another command? Like jus `ls`?

---

<div class="post-metadata">

**Author:** ![Zranz](https://avatars.discourse-cdn.com/v4/letter/z/90ced4/32.png) [@Zranz](https://discourse.julialang.org/u/Zranz)\
**Post date:** [January 14, 2019, 2:53am UTC](https://discourse.julialang.org/t/how-to-check-if-a-program-exists-in-a-system-using-julia/19604/7 "2019-01-14T02:53:01Z")

</div>

Yes, I did. `ls`, `which`, `tail`, `less`, `cat`, etc. all work. Only `type`, `hash` or ` command` fail. Even `nano` works and goes into editor mode.

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [January 14, 2019, 3:25am UTC](https://discourse.julialang.org/t/how-to-check-if-a-program-exists-in-a-system-using-julia/19604/8 "2019-01-14T03:25:17Z")

</div>

`type` is a command built into bash, so it is not provided as a separate program on many versions of Linux. You can do

```julia
read(`bash -c "type -p ls"`)

```

---

<div class="post-metadata">

**Author:** ![Zranz](https://avatars.discourse-cdn.com/v4/letter/z/90ced4/32.png) [@Zranz](https://discourse.julialang.org/u/Zranz)\
**Post date:** [January 14, 2019, 3:28am UTC](https://discourse.julialang.org/t/how-to-check-if-a-program-exists-in-a-system-using-julia/19604/9 "2019-01-14T03:28:00Z")

</div>

Thanks Ralph, I like the suggestion. Although this is the output I get:  
`UInt8[0x2f, 0x62, 0x69, 0x6e, 0x2f, 0x6c, 0x73, 0x0a]`  
Can it be helped?

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [January 14, 2019, 3:34am UTC](https://discourse.julialang.org/t/how-to-check-if-a-program-exists-in-a-system-using-julia/19604/10 "2019-01-14T03:34:02Z")

</div>

Does `chomp(read(`bash -c “type -p ls”`,String))` do what you want?

---

<div class="post-metadata">

**Author:** ![Zranz](https://avatars.discourse-cdn.com/v4/letter/z/90ced4/32.png) [@Zranz](https://discourse.julialang.org/u/Zranz)\
**Post date:** [January 14, 2019, 3:39am UTC](https://discourse.julialang.org/t/how-to-check-if-a-program-exists-in-a-system-using-julia/19604/11 "2019-01-14T03:39:26Z")

</div>

Well, `chomp(join(map(Char, read(`bash -c “type -p ls”`))))` does.  
What a mouthful, I don’t know if it can be shorter. Only chomp didn’t work, because I get an array.

---

<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:** [January 14, 2019, 4:02am UTC](https://discourse.julialang.org/t/how-to-check-if-a-program-exists-in-a-system-using-julia/19604/12 "2019-01-14T04:02:32Z")

</div>

You want `Sys.which("program")`, which searches the `PATH` directly, avoids spawning a shell, and is cross-platform.

---

<div class="post-metadata">

**Author:** ![Zranz](https://avatars.discourse-cdn.com/v4/letter/z/90ced4/32.png) [@Zranz](https://discourse.julialang.org/u/Zranz)\
**Post date:** [January 14, 2019, 4:09am UTC](https://discourse.julialang.org/t/how-to-check-if-a-program-exists-in-a-system-using-julia/19604/13 "2019-01-14T04:09:00Z")

</div>

Thanks, this is perfect.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 14, 2019, 9:25am UTC](https://discourse.julialang.org/t/how-to-check-if-a-program-exists-in-a-system-using-julia/19604/14 "2019-01-14T09:25:39Z")

</div>

You can’t, since `type` is a shell builtin. Try something like

```julia
run(`bash -c 'type -p ls'`)

```

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [March 5, 2021, 5:14pm UTC](https://discourse.julialang.org/t/how-to-check-if-a-program-exists-in-a-system-using-julia/19604/15 "2021-03-05T17:14:43Z")

</div>

This is a bit old so maybe by now there is a better way (I couldn’t find it 🙂 ). What worked for me is invoking the command and looking for `IOError`s. If the command is found and invoked but the command itself is incorrectly invoked, different kinds of errors will result.

```julia
function run(command::Cmd)
  try Base.run(command)

  catch ex
    if isa(ex, IOError)
      @error "Can not find $(command.exec[1]). Please make sure that $(command.exec[1]) is installed and accessible."
    end

    rethrow(ex)
  end
end

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 6, 2021, 9:01am UTC](https://discourse.julialang.org/t/how-to-check-if-a-program-exists-in-a-system-using-julia/19604/16 "2021-03-06T09:01:31Z")

</div>

> [@essenciary](#):
>
> What worked for me is invoking the command and looking for `IOError` s.

I am wondering if you overlooked @stevengj’s solution above.

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [March 6, 2021, 8:08pm UTC](https://discourse.julialang.org/t/how-to-check-if-a-program-exists-in-a-system-using-julia/19604/17 "2021-03-06T20:08:08Z")

</div>

I did! Thanks for pointing it out!
