Skip to content


Execute a system command

system x     system[x]

Where x is a string representing a system command and any parameters to it, executes the command and returns any result.

The system command does not include a leading \.

q)\l sp.q
q)\a                     / tables in namespace
q)count \a               / \ must be the first character
q)system "a"             / same command called with system
q)count system "a"       / this returns a result

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

q)system "pwd"

Directing output to a file

When redirecting output to a file, for efficiency purposes, avoiding using >tmpout needlessly; append a semi-colon to the command.

q)system"cat x"

is essentially the same as the shell command

$ cat x > tmpout

as kdb+ 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 semicolon

q)system"cat x > y;"

the shell interpreter considers it as two statements

$ cat x > y; > tmpout

Capture stderr output

You cannot capture the stderr output from the system call 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"

Changing working directory

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

Back to top