RunProcess and SystemShell - some built in commands don't work?

I'm running a zsh shell:

$SystemShell = "/bin/zsh"  My problem is that some basic system-level commands don't work in RunProcess. For example, echo works but exec, source, and many other build it shell commands don't: It gives an error: RunProcess::pnfd: Program source not found. Check Environment["PATH"]. >>  The docs say that by default, the subprocess will inherit its process directory and environment variables from the Wolfram Engine, so I tried setting the ProcessEnvironment with an association to match my environment vars in the terminal. But that didn't work. What am I doing wrong here, shouldn't all system level shell commands be accessible? 2 Answers RunProcess can only run commands that are separate binaries, and commands like source and exec are built into the shell, not independent of it. You could execute them by sending them to a running zsh process through a process object returned by StartProcess[$SystemShell].

• Thanks, but how can I run a process within a process. I'm trying to launch an ipython shell inside a virtualenv within the zsh shell. – M.R. Nov 1 '15 at 23:23
• @M.R. Does that work : RunProcess[$SystemShell, "StandardOutput", "source yourfilepath"] – SquareOne Nov 1 '15 at 23:29 • @SquareOne No, it gives me this: during evaluation of In[126]:= OpenRead::noopen: Cannot open ZSH-ENV... ZSH-RC.... >> Out[127]=$Failed – M.R. Nov 1 '15 at 23:46
• @SquareOne Try it with StartProcess and then try to run another command using WriteLine, it doesn't work... – M.R. Nov 2 '15 at 0:09
• @M.R. No, actually I got it work with StartProcess but not with RunProcess ... ?? – SquareOne Nov 2 '15 at 0:21

I am able to use both, RunProcess and StartProcess, to execute a built-in shell command. At least it works for me on OSX, with zsh or sh shell.

Here is a minimal example with the built-in command source (like in the OP's question).

First, let's choose our shell :

myshell = "/bin/zsh"; (* or "/bin/sh" *)


This is some trivial shell script we'd like to source:

code = "#! " <> myshell <> "\nhi () {echo \"Hello World\";}"

"#! /bin/zsh
hi () {echo \"Hello World\";}"


that we can export directly to an external file (inside for example home directory):

Export["~/file2source", code, "String"];


These are different ways to source the script in the file:

1.

RunProcess[myshell, "StandardOutput", "source ~/file2source ; hi "]


Hello World

2.1

process = StartProcess[myshell];
WriteLine[process, "source ~/file2source  ; hi"];
WriteLine[process, "exit"];


Hello World

2.2

process = StartProcess[myshell];
WriteLine[process, "source ~/file2source ; hi ; exit"];

Hello World
Comment: It seems there is a problem with the approach consisting in reading the buffer (like ReadString[process, EndOfBuffer]) when the command is contained in a notebook cell with other commands ... I'll explain that in a different post.