Main Content


Execute UNIX command and return output

For platform-independent code, use the system command.



[status,cmdout] = unix(command) calls the operating system to execute the specified command and returns the standard output of the command to cmdout.

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.

For tips and information about limitations, see system.


collapse all

Display the current folder using the cd command. A status of zero indicates that the command completed successfully. MATLAB returns a character vector containing the current folder in cmdout.

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

Input Arguments

collapse all

UNIX command, specified as a character vector. The command executes in a UNIX shell, which might not be the shell from which you started MATLAB.

Example: 'ls'

Output Arguments

collapse all

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 when command starts

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

Output of the operating system command, returned as a character vector. The system shell might not properly represent non-Unicode® characters.

Version History

Introduced before R2006a