Documentation Center

  • Trial Software
  • Product Updates

unix

Execute UNIX command and return output

Syntax

  • status = unix(command)
  • [status,cmdout] = unix(command) example
  • [status,cmdout] = unix(command,'-echo')

Description

status = unix(command) calls the UNIX® operating system to execute the specified command. The operation waits for the command to finish execution before returning the exit status of the command to the status variable.

example

[status,cmdout] = unix(command) additionally returns the standard output of the command to cmdout. This syntax is most useful for commands that do not require user input.

[status,cmdout] = unix(command,'-echo') additionally displays (echoes) the command output in the MATLAB® Command Window. This syntax is most useful for commands that require user input and that run correctly in the MATLAB Command Window.

Examples

expand all

Save UNIX Command Exit Status and Output

List all users who are currently logged in, and save the command exit status and output. Then, view the status.

command = 'who';
[status,cmdout] = unix(command);
status
status =

     0

A status of zero indicates that the command completed successfully. MATLAB returns a string containing the list of users in cmdout.

Input Arguments

expand all

command — UNIX commandstring

UNIX command, specified as a string. The command executes in a UNIX shell, which might not be the shell from which you launched MATLAB.

Example: 'ls'

Output Arguments

expand all

status — Command exit status0 | nonzero integer

Command exit status, returned as either 0 or a nonzero integer. When the command is successful, status is 0. Otherwise, status is a nonzero integer.

  • If command includes the ampersand character (&), then status is the exit status upon command launch.

  • If command does not include the ampersand character (&), then status is the exit status upon command completion.

cmdout — Output of operating system commandstring

Output of the operating system command, returned as a string.

More About

expand all

Tips

  • To execute the operating system command in the background, include the trailing character, &, in the command argument (for example, 'emacs &'). The exit status is immediately returned to the status variable. This syntax is useful for console programs that require interactive user command input while they run, and that do not run correctly in the MATLAB Command Window.

      Note:   If command includes the trailing & character, cmdout is empty.

  • The unix function redirects stdin to the invoked command, command, by default. This redirection also forwards MATLAB script commands and the keyboard type-ahead buffer to the invoked command while the unix function executes. This can lead to corrupted output when unix does not complete execution immediately. To disable stdin and type-ahead redirection, include the formatted string < /dev/null in the call to the invoked command.

Algorithms

MATLAB uses a shell program to execute the given command. It determines which shell program to use by checking environment variables on your system. MATLAB first checks the MATLAB_SHELL variable, and if either empty or not defined, then checks SHELL. If SHELL is also empty or not defined, MATLAB uses /bin/sh.

See Also

| | | |

Was this topic helpful?