# Run file and print line results as in REPL

**URL:** https://discourse.julialang.org/t/run-file-and-print-line-results-as-in-repl/53651
**Category:** General Usage
**Tags:** question
**Created:** [January 20, 2021, 2:51am UTC](https://discourse.julialang.org/t/run-file-and-print-line-results-as-in-repl/53651 "2021-01-20T02:51:46Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![RaulDurand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rauldurand/32/2044_2.png) [@RaulDurand](https://discourse.julialang.org/u/RaulDurand)
#### Post date: [January 20, 2021, 2:51am UTC](https://discourse.julialang.org/t/run-file-and-print-line-results-as-in-repl/53651/1 "2021-01-20T02:51:46Z")

</div>

How can I run a file and obtain the results in the screen as if the file were executed line by line in the REPL? For example, if I have the file “file.jl”:

```julia
a = 1
b = 1
c = a+b

```

and run it from the shell

```julia
~$ julia file.jl > output.txt

```

the file output.txt should contain:

```julia
julia> a = 1
2
julia> b = 2
3
julia> c = a+b
5

```

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [January 20, 2021, 12:09pm UTC](https://discourse.julialang.org/t/run-file-and-print-line-results-as-in-repl/53651/2 "2021-01-20T12:09:59Z")

</div>

`socat` can simulate tty input for any process:  
run `echo 'a = 2\nb = 3\nc = a + b' | socat - EXEC:'julia',pty,setsid`  
or  
`cat file.jl | socat - EXEC:'julia',pty,setsid`

---

<div class="post-metadata">

### Author: ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)
#### Post date: [January 20, 2021, 6:08pm UTC](https://discourse.julialang.org/t/run-file-and-print-line-results-as-in-repl/53651/3 "2021-01-20T18:08:52Z")

</div>

Just to check: should it be line by line or expression by expression. For example, would the following also be a valid `file.jl`?

```julia
a = 1
b = 1
c = a+b
function mysum(u, v)
    u + v
end
mysum(a, b)

```

---

<div class="post-metadata">

### Author: ![RaulDurand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rauldurand/32/2044_2.png) [@RaulDurand](https://discourse.julialang.org/u/RaulDurand)
#### Post date: [January 21, 2021, 4:01am UTC](https://discourse.julialang.org/t/run-file-and-print-line-results-as-in-repl/53651/4 "2021-01-21T04:01:51Z")

</div>

Actually, by expression.

---

<div class="post-metadata">

### Author: ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)
#### Post date: [January 21, 2021, 8:29am UTC](https://discourse.julialang.org/t/run-file-and-print-line-results-as-in-repl/53651/5 "2021-01-21T08:29:00Z")

</div>

Say `file.jl` contains

```julia
 a = 1
 b = 1
 function mysum(u, v)
     u + v
 end
 mysum(a, b)

```

We can define two functions

```julia
function repl_show(s::AbstractString)
    block = Meta.parse(s)
    args = block.args
    expressions = filter(x -> typeof(x) != LineNumberNode, args)
    for ex in expressions
        code = string(ex)
        println("julia> $code")
        out = eval(ex)
        println("$out\n")
   end
end

function repl_file(path)
    contents = read(path, String)
    s = """
    begin 
    $contents 
    end
    """
    repl_show(s)
end

```

and apply them to the file:

```julia
julia> repl_file("file.jl")

```

which gives **as output**

```julia
julia> a = 1
1

julia> b = 1
1

julia> function mysum(u, v)
    #= none:4 =#
    #= none:5 =#
    u + v
end
mysum

julia> mysum(a, b)
2

```

To “carry” these tools around across various folders or projects, you can put `repl_show` and `repl_file` in a package like ShowREPL. Then, it can be used as

```julia
julia -e 'using ShowREPL; ShowREPL.repl_file("file.jl")' > output.txt

```
