Skip to content

The q session

The q session is a read-evaluate-print loop. It evaluates a q expression and prints the result. You can use it as a calculator.

q)sum 44.95 1032 107.15
q)acos -1

The q interpreter ignores your comments.

q)2+2 3 4   / add atom to a vector
4 5 6
q)/ Pi is the arc-cosine of -1
q)pi:acos -1

Use show to set and display a value in one expression.

q)show pi:acos -1

Multiline expressions

When you key Enter, the interpreter evaluates what you just typed. There is no way in the session for you to write an expression or comment that spans multiple lines.

Scripts permit this.


System commands

You can also issue system commands. For example, to see the current print precision:


A system command may print to the session, as above, but does not return a result that can be named. To do this, use the system keyword.

q)show p:system"P"

System commands begin with a backslash. If what follows is not a q system command, it is passed to the operating system.

q)\ls -al ~/.
"total 1560"
"drwxr-xr-x+  87 sjt   staff    2784 21 Feb 10:02 ."
"drwxr-xr-x    9 root  admin     288 12 Feb 13:39 .."
"-r--------    1 sjt   staff       7 26 Feb  2018 .CFUserTextEncoding"
"-rw-r--r--@   1 sjt   staff   26628 20 Feb 14:43 .DS_Store"

Watch out for typos when issuing system commands. They may get executed in the OS.

System commands


If q cannot evaluate your expression it signals an error.

  [0]  2+"a"

The error message is terse. If the expression is within a function, the function is suspended, which allows you to investigate the error in the context in which it is evaluated.

q){x+2} "xyz"
  [1]  {x+2}
q))/ the extra ) indicates a suspended function

q))x                / x is the function argument

Use the Abort system command to cut the stack back one level.

q)/ the single ) indicates the function is off the stack
q)/ x is now undefined 
  [0]  x

Debugging, Error messages

Command-line options

The q session can be launched with parameters. The most important is a filename. Q will run it as a script.

$ cat hello.q
 title: hello-world script in q
 date: February 2020
1 "hello world";

exit 0
$ q hello.q
KDB+ 3.7t 2020.02.14 Copyright (C) 1993-2020 Kx Systems
m64/ 4()core 8192MB sjt mint.local EXPIRE 2020.04.01 #55032

hello world

Other predefined parameters set the listening port, number of slave tasks allocated, and so on.

Command-line options

Any other parameters are for you to specify and use.

$ q -foo 5432 -bar "quick brown fox"
KDB+ 3.7t 2020.02.14 Copyright (C) 1993-2020 Kx Systems
m64/ 4()core 8192MB sjt mint.local EXPIRE 2020.04.01 #55032

q).Q.opt .z.x
foo| "5432"
bar| "quick brown fox"

.Q namespace, .z namespace


End your q session with the Terminate system command.