Shell Command to make environmental variables

I am on linux and trying to execute the following in a Julia REPL:

run(export JULIA_NUM_THREADS=4)

I get the error ERROR: IOError: could not spawn export JULIA_NUM_THREADS=4: no such file or directory (ENOENT)

I can execute simpler commands like run(ls -l) successfully. I just can’t make environmental variables. This is really confusing as if I copy paste this into a terminal directly it does work. So it seems like this is a bug in Julia (or it is just really complicated to execute terminal commands in Julia).

The julia run function executes programs. The shell command export is not a program, it’s a builtin shell command. If you want to start a shell from julia you can do it explicitly with e.g. run(`/bin/bash -c "<some shell command>"`)

The command is never run with a shell. Instead, Julia parses the command syntax directly, appropriately interpolating variables and splitting on words as the shell would, respecting shell quoting syntax. The command is run as julia’s immediate child process, using fork and exec calls.

https://docs.julialang.org/en/v1/manual/running-external-programs/

2 Likes

That being said, changing the environment variables is done by writing to the ENV dictionary.

Either way, changing JULIA_NUM_THREADS in a running session won’t increase the number of running threads. You have to set that environment variable before you start julia (that also means outside startup.jl).

1 Like