How to run a function inside a julia script from bash

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

function nothing()
      println("nothing")
end

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

I know I can do it like this

function print_out( print_arg )
             println(  print_arg   )
end

print_out( ARGS[1])

but I was wondering if

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

should work’

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

function nothing()
    println("nothing")
end

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

And you can call it with a argument like this:

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:
function print_out(x)
    println(x)
end
  1. Open a Julia REPL and do:
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> 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 and here. 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:

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

and then do

% 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.

2 Likes

See the FAQ answer at How do I pass options to julia?

1 Like

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.

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

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

thanks again

1 Like

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.