Skip to content

system

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 the result as a list of character vectors.

The system command does not include a leading \.

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

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

q)system "pwd"
"/home/guest/q"
Binary output

The result is expected to be text, and is captured into a list of character vectors. As part of this capture, line feeds and associated carriage returns are removed.

This transformation makes it impractical to capture binary data from the result of the system call. Redirecting the output to a file or fifo for explicit ingestion may be appropriate in such cases.

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

system"cat x > y"

under the covers that looks like

cat x > y > tmpout

Not good. So if you add the semicolon

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 https://devblogs.microsoft.com/oldnewthing/?p=24433