# Calling a julia script from within another script

**URL:** <https://discourse.julialang.org/t/calling-a-julia-script-from-within-another-script/100989>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 29, 2023, 5:21pm UTC](https://discourse.julialang.org/t/calling-a-julia-script-from-within-another-script/100989 "2023-06-29T17:21:46Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![clairem](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@clairem](https://discourse.julialang.org/u/clairem)\
**Post date:** [June 29, 2023, 5:21pm UTC](https://discourse.julialang.org/t/calling-a-julia-script-from-within-another-script/100989/1 "2023-06-29T17:21:47Z")

</div>

I have a julia file that contains functions and code that uses those functions. I would like to create a separate julia script that is able to execute this script (and obtain one of the values that is created at the end of the script).

However, I would like the separate julia script to be able to pass a parameter to the first script when calling it. I can’t figure out how to do this part. I’m a beginner to julia, so I’d appreciate an answer in simple terms!

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [June 29, 2023, 5:27pm UTC](https://discourse.julialang.org/t/calling-a-julia-script-from-within-another-script/100989/2 "2023-06-29T17:27:26Z")

</div>

You could just include the script? And expose functions?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 29, 2023, 5:47pm UTC](https://discourse.julialang.org/t/calling-a-julia-script-from-within-another-script/100989/3 "2023-06-29T17:47:04Z")

</div>

Is a command-line parameter that you are talking about? Julia has `ARGS` that is a global vector with the command-line parameters as `String`s. You can change the first script to check the arguments there, and then call it from the second script as you can do with any script (with `run`).

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [June 29, 2023, 6:26pm UTC](https://discourse.julialang.org/t/calling-a-julia-script-from-within-another-script/100989/4 "2023-06-29T18:26:45Z")

</div>

But why would you want to do that and not include it in the first place?

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [June 29, 2023, 6:40pm UTC](https://discourse.julialang.org/t/calling-a-julia-script-from-within-another-script/100989/5 "2023-06-29T18:40:04Z")

</div>

In Julia, calling the script from another script is not at all a common practice.

Here is how you can achieve the desired behavior:

Content of script to be _called_ (let’s name it `script-a.jl`):

```julia
function fwithparam(x)
    x + 1
end

```

_Caller_ content (let’s say `script-b.jl`):

```julia
include("script-a.jl") # assuming it is in the same directory

# now - you have access to your fwithparam function
println(fwithparam(10)) # should print 11

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 30, 2023, 10:02am UTC](https://discourse.julialang.org/t/calling-a-julia-script-from-within-another-script/100989/6 "2023-06-30T10:02:31Z")

</div>

Because the interface may only be defined at script level? If you include and in the future you change the name of the functions, or how you do the things, or the language of the script then the including script will break. Other reason is that if you start using `include` in a frequent manner, you will start having problems with script C includes script B and A, and script B includes script A too, but then you have double included A and things may start to get in disarray.

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [June 30, 2023, 10:51am UTC](https://discourse.julialang.org/t/calling-a-julia-script-from-within-another-script/100989/7 "2023-06-30T10:51:46Z")

</div>

Here is how you can do it if you (for any reason) prefer this approach.

Let’s put this in the script to be called from another script (let’s say `A.jl`):

```julia
x = ARGS[1]
callx(x) = parse(Int, x) + 1
println(callx(x))

```

Now, in the script that will call/execute `A.jl`, we can add (let’s call this `B.jl`):

```julia
myx = 5
output = readchomp(`julia A.jl $myx`)
println("I received: $output from script execution of A.jl...")

```

Now, if I go in terminal and run:

```bash
julia B.jl

```

… you’ll see the script `B.jl` calling `A.jl` and you’ll get the following output:

```julia
I received: 6 from script execution of A.jl...

```

In this way, you don’t need to care about what the functions are called inside `A.jl` - you just need to make sure you are printing stuff to `stdout`.

Have fun.

---

<div class="post-metadata">

**Author:** ![xgdgsc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xgdgsc/32/608_2.png) [@xgdgsc](https://discourse.julialang.org/u/xgdgsc)\
**Post date:** [July 1, 2023, 1:28am UTC](https://discourse.julialang.org/t/calling-a-julia-script-from-within-another-script/100989/10 "2023-07-01T01:28:49Z")

</div>

Use [Home · Comonicon.jl](https://comonicon.org/stable/) for adding cli to the first script. See [Running External Programs · The Julia Language](https://docs.julialang.org/en/v1/manual/running-external-programs/#command-interpolation) to generate Cmd that includes the arguments in the second script.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [July 1, 2023, 5:03am UTC](https://discourse.julialang.org/t/calling-a-julia-script-from-within-another-script/100989/11 "2023-07-01T05:03:56Z")

</div>

> [@clairem](#):
>
> However, I would like the separate julia script to be able to pass a parameter to the first script when calling it.

It’s not clear what your goals and motivations are. I suppose you could put the functions into a single file, perhaps organized into a module, and then `include` this from other `.jl` files, as ufechner7 wrote.

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [July 5, 2023, 6:31pm UTC](https://discourse.julialang.org/t/calling-a-julia-script-from-within-another-script/100989/12 "2023-07-05T18:31:17Z")

</div>

@clairem, I see that you picked one of my posts as the solution - however, I think you wanted to pick the [other](https://discourse.julialang.org/t/calling-a-julia-script-from-within-another-script/100989/7) one where I answered specifically to your original question.

I am only writing this to make it easier for other users with similar questions that might stumble upon this thread.
