# `system`

_Execute a system command_
```syntax
system x     system[x]
```

Where `x` is a string representing a [kdb+ system command](../basics/syscmds.md) or operating system shell command, and any parameters to it. Executes the command and returns the result as a list of character vectors.

## kdb+ system commands

Refer to the [system commands reference](../basics/syscmds.md) for a full list of available commands.

> **Note — The system command does not include a leading `\`.**
>
>
```q
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
```

### 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>

## Operating system shell commands

As with `\`, if the argument is not a q command, it is executed in the shell:
```q
q)system "pwd"
"/home/guest/q"
```

> **Warning — Binary output**
>
>
>  result is expected to be text, and is captured into a list of character vectors. 
> part of this capture, line feeds and associated carriage returns are removed. 
>
> s transformation makes it impractical to capture binary data from the result of the system call. 
> irecting the output to a 
> le](read1.md) or 
> fo](../kb/named-pipes.md) 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
q)system"cat x"
```

is essentially the same as the shell command
```bash
cat x > tmpout
```

as kdb+ tries to capture the output.
So if you do
```q
q)system"cat x > y"
```

under the covers that looks like
```bash
cat x > y > tmpout
```

Not good. So if you add the semicolon
```q
q)system"cat x > y;"
```

the shell interpreter considers it as two statements
```bash
cat x > y; > tmpout
```

### Capture stderr output

You cannot capture the stderr output from the system call directly, but a workaround is
```q
q)/ 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"        

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