From Kx Wiki
Jump to: navigation, search

system (system command function)

The function system executes system commands, and is a cover for the \ system command.


q)r:system X

system is defined as:


The benefit of using system rather than \ is that it can be used as an ordinary function, and returns a result. For example, the following shows that the result of \w (workspace information) cannot be assigned, but the result can be obtained using system:

107728 67108864 67108864 0 0j
q)a:system "w"
107872 67108864 67108864 0 0j

As with \, if the argument is not a q command, it is executed in the shell:

q)system "pwd"

When redirecting output to a file, for efficiency purposes, avoiding using /tmp needlessly, append a semi-colon to the command:

q)system"cat x"
is essentially the same as the shell command
$cat x > tmpout
as q tries to capture the output.
So if you do
q)system"cat x > y"
under the covers that looks like
$cat x > y > tmpout
not good. So if you add the semi colon
q)system"cat x > y;"
the shell interpreter considers it as 2 statements
$cat x > y; > tmpout

Can I capture the stderr output from the system call? Not directly, but a workaround is

/ force capture to a file, and cat the file
q)system"ls egg > file 2>&1;cat file"                                                                                                         
"ls: egg: No such file or directory"

/ try and fails to capture the text
q)@[system;"ls egg";{0N!"error - ",x;}]                                                                                                       
ls: egg: No such file or directory
"error - os"

In the event of an unexpected change to the working directory, Windows users please note

See also

For a complete list of functions, see the kdb+ Function Reference.

Personal tools