# Which code does in Julia script need to be written to make code from REPL running as script?

**URL:** https://discourse.julialang.org/t/which-code-does-in-julia-script-need-to-be-written-to-make-code-from-repl-running-as-script/115599
**Category:** New to Julia
**Tags:** question
**Created:** [June 13, 2024, 1:41pm UTC](https://discourse.julialang.org/t/which-code-does-in-julia-script-need-to-be-written-to-make-code-from-repl-running-as-script/115599 "2024-06-13T13:41:27Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![oOosys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ooosys/32/209566_2.png) [@oOosys](https://discourse.julialang.org/u/oOosys)
#### Post date: [June 13, 2024, 1:41pm UTC](https://discourse.julialang.org/t/which-code-does-in-julia-script-need-to-be-written-to-make-code-from-repl-running-as-script/115599/1 "2024-06-13T13:41:27Z")

</div>

What need to be explicit mentioned (written out as code) in a Julia script which does not need to be mentioned while using Julia REPL? It seems that for example code which runs in the REPL taken 1:1 to a script does not work because of missing symbol names.

---

<div class="post-metadata">

### Author: ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)
#### Post date: [June 13, 2024, 2:18pm UTC](https://discourse.julialang.org/t/which-code-does-in-julia-script-need-to-be-written-to-make-code-from-repl-running-as-script/115599/2 "2024-06-13T14:18:28Z")

</div>

At least `InteractiveUtils` is automatically loaded in the REPL, so you need to put:

```julia
using InteractiveUtils

```

in scripts in order to be able to use things like `versioninfo()` which work automatically in the REPL.

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [June 13, 2024, 2:21pm UTC](https://discourse.julialang.org/t/which-code-does-in-julia-script-need-to-be-written-to-make-code-from-repl-running-as-script/115599/3 "2024-06-13T14:21:04Z")

</div>

In the REPL you have “soft scope”.

[https://docs.julialang.org/en/v1/manual/variables-and-scoping/](https://docs.julialang.org/en/v1/manual/variables-and-scoping/)

That means this works in the REPL:

```julia-repl
julia> x = 0
0

julia> for i in 1:10
           x += 1
       end

julia> x
10

```

However, this will not work:

```julia
$ julia -E "
x = 0
for i in 1:10
    x += 1
end
x
"
┌ Warning: Assignment to `x` in soft scope is ambiguous because a global variable by the same name exists: `x` will be treated as a new local. Disambiguate by using `local x` to suppress this warning or `global x` to assign to the existing global variable.
└ @ none:4
ERROR: UndefVarError: `x` not defined
Stacktrace:
 [1] top-level scope
   @ ./none:4

```

You need to add the global keyword.

```julia
$ julia -E "
x = 0
for i in 1:10
    global x += 1
end
x
"
10

```

My advice is to focus writing functions rather than scripts in Julia.

The following works.

```julia-repl
julia> function foo()
           x = 0
           for i in 1:10
               x += 1
           end
           return x
       end
foo (generic function with 1 method)

julia> foo()
10

```

And also works outside the REPL.

```julia
$ julia -E "
function foo()
     x = 0
     for i in 1:10
         x += 1
     end
     return x
end
foo()
"
10

```

---

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [June 13, 2024, 3:19pm UTC](https://discourse.julialang.org/t/which-code-does-in-julia-script-need-to-be-written-to-make-code-from-repl-running-as-script/115599/4 "2024-06-13T15:19:54Z")

</div>

> [@mkitti](#):
>
> [Scope of Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/variables-and-scoping/)

The development version of the documentation has additional information about this common confusion: [Scope of Variables · The Julia Language](https://docs.julialang.org/en/v1.12-dev/manual/variables-and-scoping/)

---

<div class="post-metadata">

### Author: ![oOosys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ooosys/32/209566_2.png) [@oOosys](https://discourse.julialang.org/u/oOosys)
#### Post date: [June 13, 2024, 7:53pm UTC](https://discourse.julialang.org/t/which-code-does-in-julia-script-need-to-be-written-to-make-code-from-repl-running-as-script/115599/5 "2024-06-13T19:53:41Z")

</div>

Isn’t there a preliminary script which needs to be imported into a script in its first line to provide what REPL comes with? Where does the REPL get its initialization from?
