# How to run a function inside a julia script from bash

**URL:** <https://discourse.julialang.org/t/how-to-run-a-function-inside-a-julia-script-from-bash/74683>\
**Category:** New to Julia\
**Created:** [January 15, 2022, 8:46pm UTC](https://discourse.julialang.org/t/how-to-run-a-function-inside-a-julia-script-from-bash/74683 "2022-01-15T20:46:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [January 15, 2022, 8:46pm UTC](https://discourse.julialang.org/t/how-to-run-a-function-inside-a-julia-script-from-bash/74683/1 "2022-01-15T20:46:27Z")

</div>

I’m just starting out and can’t seem to find how to run a function inside a julia script from a bash prompt.

so if I have a script ( below) print\_it.jl and want to print out “hello world” using the function print\_out how do I do that please using a bash shell. I have completely failed trying to look this up.

$julia print\_it.jl

```julia
function nothing()
      println("nothing")
end

function print_out( ARGS[1])
             println( ARGS[1] )
end

```

I know I can do it like this

```julia
function print_out( print_arg )
             println( print_arg )
end

print_out( ARGS[1])

```

but I was wondering if

```julia
$julia print_it.print_out().jl "hello world"  

```

should work’

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 15, 2022, 9:00pm UTC](https://discourse.julialang.org/t/how-to-run-a-function-inside-a-julia-script-from-bash/74683/2 "2022-01-15T21:00:38Z")

</div>

The short answer is, first that your script should look like this:

```julia
function nothing()
    println("nothing")
end

function print_out()
    println(ARGS[1])
end

```

And you can call it with a argument like this:

```bash
julia -e "include(\"./hello.jl\"); print_out()" -- "hello world"

```

The long answer is that this is, of course, excendendly complicated, because it is not the way you should be developing in Julia.

In Julia, do the following:

1. Create your script, for example, like this:

```julia
function print_out(x)
    println(x)
end

```

1. Open a Julia REPL and do:

```julia
julia> include("./hello.jl")
print_out (generic function with 1 method)

julia> print_out("hello world")
hello world

```

Now, better, install `Revise`, and do:

```julia
julia> using Revise

julia> includet("./hello.jl") # note the "t" in includet

julia> print_out("hello world")
hello world

```

The cool thing now is that if you modify the script and save it, and just run the function `print_out` again _without leaving the REPL_, the modification of the function will be taken into account.

I have some personal notes on this [here](https://m3g.github.io/JuliaNotes.jl/stable/workflow/) and [here](https://m3g.github.io/JuliaNotes.jl/stable/modules/). Of course you will find much more information in other sources, like here and in the docs of the Revise package, for example.

Edit: One alternative to the “first answer” is to call the function from within the script, for example:

```julia
function print_out()
    println(ARGS[1])
end
print_out()

```

and then do

```julia
% julia hello.jl -- "hello world"
hello world

```

But this is not the most common approach for development, or use of Julia, which is the “Revise” approach mentioned, with a persistent REPL.

---

<div class="post-metadata">

**Author:** ![devel-chm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devel-chm/32/3572_2.png) [@devel-chm](https://discourse.julialang.org/u/devel-chm)\
**Post date:** [January 15, 2022, 9:16pm UTC](https://discourse.julialang.org/t/how-to-run-a-function-inside-a-julia-script-from-bash/74683/3 "2022-01-15T21:16:03Z")

</div>

See the FAQ answer at [How do I pass options to julia?](https://docs.julialang.org/en/v1.6/manual/faq/#How-do-I-pass-options-to-julia-using-#!/usr/bin/env?)

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [January 15, 2022, 9:21pm UTC](https://discourse.julialang.org/t/how-to-run-a-function-inside-a-julia-script-from-bash/74683/4 "2022-01-15T21:21:58Z")

</div>

Hi there  
thank you for your excellent reply. I have been using the REPL approach that you covered. I wanted to know how to get at the print\_function from the bash shell just in case I ever needed to do something like that in the future.

```julia
function print_out(x)
    println(x)
end

    Open a Julia REPL and do:

julia> include("./hello.jl")
print_out (generic function with 1 method)

julia> print_out("hello world")
hello world

```

and I did include the other approach you mentioned in the question

```julia
function print_out( print_arg )
             println( print_arg )
end

print_out( ARGS[1])

```

what I was pondering is how to just get at the print\_function from the command line and you did a great job answering it. All the examples I read had how to do so inside the REPL but not the bash shell. Thank you for that.

I understand that it’s NOT the manner in which I should be developing julia packages BUT it’s useful information to me. It gives me something concrete to look and and understand in context. I’ll spend sometime looking at your answer and breaking it down. I was thinking about a pipeling approach where a cronjob starts a python script that pipes it’s output into a julia function which outputs the required input into a c program.

Your revise notes are EXCELLENT! [excellent development notes](https://m3g.github.io/JuliaNotes.jl/stable/workflow/)

thanks again

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [January 15, 2022, 9:26pm UTC](https://discourse.julialang.org/t/how-to-run-a-function-inside-a-julia-script-from-bash/74683/5 "2022-01-15T21:26:44Z")

</div>

WOW I completely forgot to look there!!! I must be losing it after the huge saturday breakfast at Wildberry. Got to lay off the carbs!!! thanks for the GREAT reminder.
