4.35 Operating System Interaction
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
      • Built-in Predicates
        • Operating System Interaction
          • shell/1
          • shell/2
          • getenv/2
          • setenv/2
          • unsetenv/1
          • setlocale/3
          • Windows-specific Operating System Interaction
          • Apple specific Operating System Interaction
          • Dealing with time and date
          • Controlling the swipl-win.exe console window
    • Packages
Availability:built-in
shell(+Command, -Status)
Execute Command on the operating system. Command is given to the Bourne shell (/bin/sh). Status is unified with the exit status of the command.

On Windows, shell/[1,2] executes the command using the CreateProcess() API and waits for the command to terminate. If the command ends with a & sign, the command is handed to the WinExec() API, which does not wait for the new task to terminate. See also win_exec/2 and win_shell/2. Please note that the CreateProcess() API does not imply the Windows command interpreter (cmd.exe and therefore commands that are built in the command interpreter can only be activated using the command interpreter. For example, a file can be copied using the command below.

?- shell('cmd.exe /C copy file1.txt file2.txt').

Note that many of the operations that can be achieved using the shell built-in commands can easily be achieved using Prolog primitives. See make_directory/1, delete_file/1, rename_file/2, etc. The clib package provides library(filesex), implementing various high level file operations such as copy_file/2. Using Prolog primitives instead of shell commands improves the portability of your program.

The library library(process) provides process_create/3 and several related primitives that support more fine-grained interaction with processes, including I/O redirection and management of asynchronous processes.