6.32 Passing Commands to the Operating System

Gforth allows you to pass an arbitrary string to the host operating system shell (if such a thing exists) for execution.

sh ( "..." –  ) gforth-0.2 “sh”

Execute the rest of the command line as shell command(s). Afterwards, $? produces the exit status of the command.

system ( c-addr u –  ) gforth-0.2 “system”

Pass the string specified by c-addr u to the host operating system for execution in a sub-shell. Afterwards, $? produces the exit status of the command. The value of the environment variable GFORTHSYSTEMPREFIX (or its default value) is prepended to the string (mainly to support using command.com as shell in Windows instead of whatever shell Cygwin uses by default; see Environment variables).

sh-get ( c-addr u – c-addr2 u2  ) gforth-1.0 “sh-get”

Run the shell command addr u; c-addr2 u2 is the output of the command. The exit code is in $?, the output also in sh$ 2@.

$? ( – n  ) gforth-0.2 “dollar-question”

Value – the exit status returned by the most recently executed system command.

getenv ( c-addr1 u1 – c-addr2 u2 ) gforth-0.2 “getenv”

The string c-addr1 u1 specifies an environment variable. The string c-addr2 u2 is the host operating system’s expansion of that environment variable. If the environment variable does not exist, c-addr2 u2 specifies a string 0 characters in length.