George
George

Reputation: 7317

Parsing a Shell Command with Environment Variables in Haskell

Haskell's System.Process provides us with a way to convert a String into a shell command via shell:

System.Process.createProcess (System.Process.shell $ "a_command with some arguments")

The problem is that shell doesn't parse environment variables. Is there a library command which converts a shell string into a shell call, that is sensitive to environment variables? Something like

System.Process.createProcess (shellWithEnv "ENVIRONMENT_VARIABLE=value a_command with some arguments")

?

EDIT: More evidence shell does not pick this up:

GHCI> shell "ENV=value echo hi"
CreateProcess {cmdspec = ShellCommand "ENV=value echo hi", cwd = Nothing, env = Nothing, std_in = Inherit, std_out = Inherit, std_err = Inherit, close_fds = False, create_group = False, delegate_ctlc = False, detach_console = False, create_new_console = False, new_session = False, child_group = Nothing, child_user = Nothing, use_process_jobs = False}

Here env = Nothing.

Upvotes: 1

Views: 227

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476813

shell :: String -> CreateProcess is just a function that constructs a CreateProcess object. You can update the env attribute CreateProcess data of the record with:

import System.Process(CreateProcess(env), createProcess, shell)

createProcess ((\x -> x {env=Just [("ENVIRONMENT_VARIABLE", "value")]}) (shell "a_command with some arguments"))

Upvotes: 4

Related Questions