export
is shell built-in; you can not call it outside a shell. So
what you want to do instead is run your command with another
environment. This can be done in sh
with the :env
option. E.g.
(-> (shell/sh "sh" "-c" "echo $X" :env {"X" "Hello"}) :out)
edit: global state for environment
At least on the JVM, there is no easy way to change the
environment.
So you are better off, writing your own function to do the calls and
merge with your own global environment.
This example uses an atom to keep the environment around:
(def sh-env (atom {}))
(defn export!
[m]
(swap! sh-env merge m))
(defn sh
([cmd]
(sh cmd {}))
([cmd env]
(apply shell/sh (concat cmd [:env (merge @sh-env env)]))))
;;;
(def echo ["sh" "-c" "echo $X"])
(prn (sh echo))
(export! {"X" "Hello"})
(prn (sh echo))
(prn (sh echo {"X" "Goodbye"}))
(export! {"X" "Goodbye"})
(prn (sh echo))
; {:exit 0, :out "\n", :err ""}
; {:exit 0, :out "Hello\n", :err ""}
; {:exit 0, :out "Goodbye\n", :err ""}
; {:exit 0, :out "Goodbye\n", :err ""}