# How to execute julia code in a bash shell script itself

**URL:** <https://discourse.julialang.org/t/how-to-execute-julia-code-in-a-bash-shell-script-itself/30848>\
**Category:** General Usage\
**Created:** [November 8, 2019, 1:20am UTC](https://discourse.julialang.org/t/how-to-execute-julia-code-in-a-bash-shell-script-itself/30848 "2019-11-08T01:20:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [November 8, 2019, 1:20am UTC](https://discourse.julialang.org/t/how-to-execute-julia-code-in-a-bash-shell-script-itself/30848/1 "2019-11-08T01:20:40Z")

</div>

```julia
$ cat zzz
#!/bin/bash
/usr/bin/tail +4 "$0" | julia
exit
println("hello world from a bash script")
versioninfo()

```

Dont forget to

```julia
$ chmod u+x zzz

```

Sample run

```julia
$ ./zzz
hello world from a bash script
Julia Version 1.2.0
Commit c6da87ff4b (2019-08-20 00:03 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin18.6.0)
  CPU: Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, nehalem)

```

```julia
$ which julia
/usr/local/bin/julia

$ ls -l /usr/local/bin/julia
lrwxr-xr-x 1 ssiew admin 62 23 Aug 18:42 /usr/local/bin/julia -> /Applications/Julia-1.2.app/Contents/Resources/julia/bin/julia

```

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [November 8, 2019, 6:15am UTC](https://discourse.julialang.org/t/how-to-execute-julia-code-in-a-bash-shell-script-itself/30848/2 "2019-11-08T06:15:03Z")

</div>

Julia will also read a script on stdin:

```bash
#!/bin/bash
echo "From bash"

echo '
println("hello world from Julia")
versioninfo()
' | julia

echo "Back in bash"

```

Use of the `'` character will have to be escaped though.

If you want an executable script but you don’t know where your julia will be installed, there’s also this standard trick (`chmod u+x test.jl`):

```julia
#!/usr/bin/env julia
using InteractiveUtils
println("hello world from Julia")
versioninfo()

```

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [November 8, 2019, 6:20am UTC](https://discourse.julialang.org/t/how-to-execute-julia-code-in-a-bash-shell-script-itself/30848/3 "2019-11-08T06:20:03Z")

</div>

Actually it seems the stdin trick is Julia acting as a (cut down) REPL so that’s probably not a lot of good.

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [November 8, 2019, 7:29am UTC](https://discourse.julialang.org/t/how-to-execute-julia-code-in-a-bash-shell-script-itself/30848/4 "2019-11-08T07:29:59Z")

</div>

Here is yet another scripting trick I PR’ed to the document [https://docs.julialang.org/en/latest/manual/faq/#man-scripting-1](https://docs.julialang.org/en/latest/manual/faq/#man-scripting-1)

```julia
#!/bin/bash
#=
exec julia --color=yes --startup-file=no -e 'include(popfirst!(ARGS))' \
    "${BASH_SOURCE[0]}" "$@"
=#

@show ARGS # put any Julia code here

```

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [November 9, 2019, 7:54am UTC](https://discourse.julialang.org/t/how-to-execute-julia-code-in-a-bash-shell-script-itself/30848/5 "2019-11-09T07:54:15Z")

</div>

```julia
$ cat <<EOF | julia
versioninfo()
EOF

```

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [November 9, 2019, 11:08pm UTC](https://discourse.julialang.org/t/how-to-execute-julia-code-in-a-bash-shell-script-itself/30848/6 "2019-11-09T23:08:07Z")

</div>

Apparently this acts as a repl though (displaying the result of every top level expression) which might not be what you want.

There’s a trick to avoid having to escape the `$` in a heredoc by quoting the marker:

```bash
$ cat <<"EOF" | julia
> msg = "Hello"
> println("$msg world")
> EOF

# Output
"Hello"
Hello world

```

---

<div class="post-metadata">

**Author:** ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)\
**Post date:** [November 10, 2019, 12:12am UTC](https://discourse.julialang.org/t/how-to-execute-julia-code-in-a-bash-shell-script-itself/30848/7 "2019-11-10T00:12:27Z")

</div>

Can we please sticky this thread somewhere or write up these tricks in some docs… I needed this last year at my last job really bad. Also one of the sexiest selling points of Julia to linux users is it’s Bash interop.

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [November 10, 2019, 1:48am UTC](https://discourse.julialang.org/t/how-to-execute-julia-code-in-a-bash-shell-script-itself/30848/8 "2019-11-10T01:48:40Z")

</div>

Maybe in [Documentation · The Julia Language](https://docs.julialang.org/en/v1/manual/documentation/index.html)

we can have a section under “Developer Documentation” called “Julia and the Bash shell”

---

<div class="post-metadata">

**Author:** ![mkarikom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkarikom/32/7096_2.png) [@mkarikom](https://discourse.julialang.org/u/mkarikom)\
**Post date:** [April 30, 2021, 4:18pm UTC](https://discourse.julialang.org/t/how-to-execute-julia-code-in-a-bash-shell-script-itself/30848/9 "2021-04-30T16:18:12Z")

</div>

Is there a way to open a REPL from inside the script (to get a debugging environment)?
