System commands

System commands control the q environment. They have the form:

\cmd [p]

for some command cmd, and optional parameter list p.
.Q.opt (command parameters), .Q.x (non-command parameters)

Commands with optional parameters that set values, will show the current values if the parameters are elided.

Some system commands have equivalent command-line parameters.

Call a system command in a q expression

To call a system command in a q expression, use system.

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

\a (tables)

Syntax: \a

Syntax: \a [namespace]

Lists tables in the given namespace, default current namespace.

q)tt:([]dd:12 34)
q).nn.vv:([]uu:12 45)
q)\a .n
q)\a .nn
q)\d .nn

\b (views)

Syntax: \b [namespace]

Lists all dependencies (views).



\B (pending views)

Syntax: \B [namespace]

Lists all pending dependencies (views), i.e. dependencies not yet referenced, or not referenced after their referents have changed.

q)a::x+1          / a depends on x
q)\B              / the dependency is pending
q)\B              / still pending after x is defined
q)a               / use a
q)\B              / no longer pending

\c (console size)

Syntax: \c

Syntax: \c [h,w]

Sets console height and width. This is the same as the -c command line parameter.

These settings determine when q elides output with ..


You usually don't need to set this. If the environment variables LINES and COLUMNS are found they'll be taken as the default value. See Bash documentation for shopt parameter checkwinsize to make sure they're reset as needed.

45 160
q)\c 5 5
10 10
q)til each 20+til 10
0 1 2 3..
0 1 2 3..
0 1 2 3..
0 1 2 3..
0 1 2 3..
0 1 2 3..
0 1 2 3..

\C (HTTP size)

Syntax: \C [h,w]

Sets the HTTP height and width. This is the same as command-line option -C.

\cd (change directory)

Syntax: \cd [name]

Changes the current directory.

~/q$ q
KDB+ 2.6 2010.05.10 Copyright (C) 1993-2010 Kx Systems
q)\cd /home/guest/dev

\d (directory)

Syntax: \d [namespace]

Sets the current namespace (also known as directory or context). The namespace can be empty, and a new namespace is created when an object is defined in it. The prompt indicates the current namespace.

q)\d                  / default namespace
q)\d .o               / change to .o
q.o)\d .              / return to default
q)key`                / lists namespaces other than .z
q)\d .s               / change to non-existent namespace
q.s)key`              / not yet created
q.s)a:1               / create object, also creates namespace

\e (error trap clients)

Syntax: \e [0|1]

This enables error trapping for client requests. The default is 0 (off).

When a client request has an error, by default the server clears the stack. This is appropriate for production use as it enables the server to continue processing other client requests.

For development, you can set \e 1 to enable debugging on the server. In this case, the server suspends on an error, and does not process other requests until the stack is cleared.

\f (functions)

Syntax: \f [namespace]

Lists functions in the given namespace, default current namespace.

q)\f .h
q){x where x like"ht??"}system"f .h"

\g (garbage collection mode)

Syntax: \g [mode]

Since V2.7 2011.02.04. Switch garbage collection between immediate (1) and deferred (0) modes.
-g command-line option

\l (load file or directory)

Syntax: \l name

The parameter can be a script filename or a directory. A script is loaded, and a directory database is opened. When q opens a directory, it changes its current directory to it. This allows reloading the current database using \l .. If the directory is specified as ., any scripts in that directory will be ignored; this is to allow (re)loading of data only.

If a file or directory under the path being loaded has a dollar-sign suffix then it is also ignored. e.g. db/tickdata/myfile$ and db/tickdata/mydir$ would be ignored on \l db/tickdata or on \l . if db/tickdata is the current directory.

q)\l sp.q            / load sp.q script
q)\a                 / tables defined in sp.q
q)\l db/tickdata     / load the data found in db/tickdata
q)\a                 / with tables quote and trade

.Q.l (load)

\o (offset from UTC)

Syntax: \o [n]

Sets the local time offset, as hours from UTC, or as minutes if abs[n]>23. Initially, the value is 0N, meaning that the machine's offset is used.

q).z.p                        / UTC
q).z.P                        / local time is UTC + 8
q)\o -5                       / set local time as UTC - 5
q)\o 390                      / set local time as UTC + 6:30

This corresponds to the -o command line parameter.

\p (port)

Syntax: \p [i]

Sets the listening port number. The default is 0 (no listening port). The port must be available and the process must have permission for the port.

A negative parameter sets a multi-threaded port and if used it must be the initial and only mode of operation, i.e. do not dynamically switch between positive port and negative port.

A parameter of 0W means pick a random available port within the range 32768–60999.

q)\p 5010     / set port 5010
q)\p 0W       / pick a random available port within the range 32768 - 60999
q)\p 0        / turn off listening port

This corresponds to the -p command line parameter.

\P (precision)

Syntax: \P [n]

Sets display precision for floating point numbers, i.e. the number of digits shown.
-P command-line option

\r (replication master)

Syntax: \r

This should not be executed manually otherwise it can disrupt replication. It is executed automatically by the replicating process on the master process, and returns the log file name and log file count.
-r command-line option

\r (rename)

Syntax: \r src dst

This renames file src to dst. It is equivalent to the Unix mv command, or the windows move command (except that it will not rename to a different disk drive).

\s (number of slaves)

Syntax: \s

This queries the number of slaves, set with the -s command line parameter (query only).

\S (random seed)

Syntax: \S [n]

Sets the random number seed. The parameter must be a non-zero integer. Note that \S is not updated as the random-number generator is used.

q)\S              / default
8 1 9 5 4
6 6 1 8 5
q)\S -314159      / restore default seed
q)5?10            / same random numbers generated
8 1 9 5 4
q)\S              / seed is not updated


Since V3.1 2013.08.19 the behaviour is as follows.

Rng is thread-local. \S 1234 sets the seed for the rng for the main thread only. The rng in a slave thread is assigned a seed based on the slave thread number. In multithreaded input mode, the seed is based on socket descriptor. Instances started on ports 20000 thru 20099 (slave procs, used with e.g. q -s -4 have the main thread’s default seed based on the port number.

\t (timer)

Syntax: \t [p]

This command has two different uses depending on the parameter given.

An integer parameter is the number of milliseconds between timer ticks. If 0, the timer is turned off, otherwise the timer is turned on and the first tick given. On each tick, the function assigned to .z.ts is executed.

q)\t                           / default off
q)\t 1000                      / tick each second
\t 0                           / turn off

A parameter of a q expression other than a single integer is executed and the execution time shown in milliseconds.

q)\t log til 100000            / milliseconds for log of first 100000 numbers
q)\t do[100;log til 100000]    / timing for 100 repetitions
q)\t:100 log til 100000    / timing for 100 repetitions, new syntax of "\t:n expr" since 3.0 2011.11.22

The tick timer usage corresponds to the -t command line parameter.

\ts (time and space)

Syntax: \ts exp

Executes the expression and shows the execution time in milliseconds and the space used in bytes.

q)\ts log til 100000
7 2621568j

Since 3.1 2014.02.07

q)\ts:10000 log til 1000           /same as \ts do[10000; log til 1000]
329 24672

\T (timeout)

Syntax: \T [n]

This sets the client execution timeout, as the integer number of seconds a client call will execute before timing out, default 0 (no timeout). Note this is in seconds, not milliseconds like \t.

This corresponds to the -T command line parameter.

\u (reload user password file)

Syntax: \u

When q is invoked with the -u parameter specifying a user password file, then \u will reload the password file. This allows updates to the password file while the server is running.

\v (variables)

Syntax: \v [namespace]

Lists the variables in the given namespace, default current namespace.

q)\v .h
q){x where x like"????"}system"v .h"

\w (workspace)

Syntax: \w [0]

If there is no parameter, lists current memory usage, as a list of 6 long integers:

  • [0] number of bytes allocated
  • [1] bytes available in heap
  • [2] maximum heap size so far
  • [3] maximum bytes available, given in -w command line parameter
  • [4] mapped bytes
  • [5] physical memory

If there is a parameter, returns a pair:

  • [0] number of internalized symbols
  • [1] corresponding memory usage
168144 67108864 67108864 0 0 8589934592j
q)\w 0
577 25436j

The function .Q.w formats all this information:

used| 168304
heap| 67108864
peak| 67108864
wmax| 0
mmap| 0
mphy| 8589934592
syms| 577
symw| 25436

.Q.w (memory stats)

\W (week offset)

Syntax: \W [n]

Specifies the start of week offset, where 0 is Saturday. The default is 2 = Monday.

This corresponds to the -W command line parameter.

\x (expunge)

Syntax: \x .z.p\*

By default, callbacks like .z.po are not defined in the session. After they have been assigned, you can restore the default using \x to delete the definition that was made.

q).z.pi                       / default has no user defined function
q).z.pi:{">",.Q.s value x}    / assign function
q)\x .z.pi                    / restore default

N.B. This works only for .z.p* variables defined in k before q.k is loaded. e.g. as is defined in q.k, there is no default for it to be reset to.

\z (date parsing)

Syntax: \z [0|1]

Specifies the format for date parsing. 0 is "mm/dd/yyyy" and 1 is "dd/mm/yyyy".

q)\z 1

\1 & \2 (redirect)

Syntax: \1 filename
Syntax: \2 filename

\1 and \2 allow redirecting stdout and stderr to files from within the q session. The files and intermediate directories are created if necessary.

~/q$ rm -f t1.txt t2.txt
~/q$ l64/q
KDB+ 2.6 2010.05.10 Copyright (C) 1993-2010 Kx Systems
q)\1 t1.txt              / stdout
q)\2 t2.txt              / stderr
til 10
2 + "hello"
~/q$ cat t1.txt          / entry in stdout
0 1 2 3 4 5 6 7 8 9
~/q$ cat t2.txt          / entry in stderr

\_ (hide q code)

Syntax: \_ [scriptname]

This command has two different uses depending on whether a parameter is given.

If no parameter, then \_ checks if client write access is blocked.
-b command-line option

If a parameter is given, it should be a scriptname and \_ f.q makes a runtime script f.q_. The q code cannot be viewed or serialized.

~/q$ echo "a:123;f:{x+2*y}" > t1.q
~/q$ l64/q
KDB+ 2.6 2010.05.10 Copyright (C) 1993-2010 Kx Systems
q)\_ t1.q               / create locked script
q)\l t1.q_              / can be loaded as usual
q)a                     / definitions are correct
q)f[10;1 2 3]
12 14 16
q)f                     / q code cannot be displayed
q)-8!f                  / or serialized
q)read1`:t1.q_          / file contents are scrambled

\ (terminate)

If there is a suspension, this exits one level of the suspension. Otherwise, it toggles between q and k mode. (To switch languages from inside a suspension, type "\".)

q))\                         / clear suspension (only one level)
q)\                          / toggle to k mode
0 1 2 3 4
  \                          / toggle to q mode

\ (toggle q/k)

In the interactive session \ toggles between the q and k interpreters.


\\ (quit)

Syntax: \\

  • In the interactive session type \\ at the prompt to quit the session.
  • Inside a function, use value"\\\\" or exit 0 for the same result.
    exit, value, .z.exit


The text following \\ and white space is ignored by q. This is often useful in scripts where \\ can be followed by comments or usage examples.

OS Commands

If an expression begins with \ but is not recognized as a system command, then it is executed as an OS command.

q)\ls                 / usual ls command


This means that typos can get passed to the OS.

When you are run rm -r / you are have of many problem, but Big Data is not of one of them. — DevOps Borat


Syntax: system x

where x is a system command, executes it and returns its result.

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"

Directing output to a file

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

Capture stderr output

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"

Changing working directory in Windows

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