Suppress printing methods in a piped script

I am writing a short script in Julia, which is piped in from an encompassing Bash script. I want to suppress printing of “f (generic function with 1 method)” outputs, as in

$ cat <<EOF | julia
> foo() = 1
> EOF
foo (generic function with 1 method)

but I want to keep stdout and stderr as is.

Can you terminate each line with ;? I assume this ends up launching the REPL, and passing your input as an actual file instead of through stdin is not an option due to your execution environment.

That was the first thing I tried, but it does not help (should have said).

bash-5.2$ cat <<EOF | julia
> foo() = 1;
> EOF
foo (generic function with 1 method)

If necessary I can export to a temporary file but the environment may not support that, so I would prefer not to if there is another solution.

Is there a command line switch for non-interactive mode? The opposite of -i.

A workaround is putting the entire script in a let block, as in

cat <<EOF | julia
> let
>     foo(x) = x + 1
>     println(foo(3))
>     nothing
> end
> EOF
4

The let block isn’t even necessary, just ending the code with nothing does the trick.

Edit: in fact even that isn’t necessary since println returns nothing already.

Edit2: the nothing (or println) needs to be on the same line as the definition, separated by semicolon.

I don’t think that is correct, as the REPL prints line by line. Eg

bash-5.2$ cat <<EOF | julia
> foo() = 1
> nothing
> EOF
foo (generic function with 1 method)

See my second edit above

bash-3.2$ cat <<EOF | julia
> foo(x)=x+1
> println(foo(1))
> EOF
foo (generic function with 1 method)
2

vs

bash-3.2$ cat <<EOF | julia
> foo(x)=x+1; nothing
> EOF
bash-3.2$ cat <<EOF | julia
> foo(x)=x+1; println(foo(1))
> EOF
2

Just use julia - to treat stdin as a script rather than as interactive inputs. e.g. with your heredoc example:

% cat <<EOF | julia -
foo() = 1
EOF

gives no output. (This is a standard Unix idiom.)

3 Likes