Executing Shell Commands
PsySH can execute system shell commands directly from the REPL using theshell_exec() function or backticks.
Using shell_exec()
Using Backticks
Displaying Output Directly
Usepassthru() to stream output directly to the console:
Terminal Integration
Terminal State Management
PsySH manages terminal settings usingstty on Unix-like systems to provide features like:
- Raw input mode for character-by-character reading
- Signal handling for Ctrl+C interruption
- Echo control for password input
src/ExecutionLoop/ProcessForker.php:232):
Signal Handling
PsySH configures terminal signals to allow Ctrl+C to interrupt code execution:Console Window Detection
PsySH detects your terminal dimensions for proper output formatting.On Windows
Uses themode con command:
On Unix/Linux/macOS
Usestput commands:
Working Directory
Setting Working Directory
Use the--cwd option to start PsySH in a specific directory:
Changing Directory During Session
You can usechdir() to change the working directory:
Changing the working directory affects subsequent shell commands and file operations, but doesn’t change autoloading paths.
Environment Variables
Reading Environment Variables
Setting Environment Variables
PsySH-Specific Environment Variables
PsySH recognizes several environment variables:| Variable | Purpose | Example |
|---|---|---|
PSYSH_CONFIG | Config file path | ~/.psysh.php |
PSYSH_TRUST_PROJECT | Trust mode | true, false |
PSYSH_UNTRUSTED_PROJECT | Internal trust hint | (set by launcher) |
bin/psysh:85-96:
Process Forking
Whenpcntl is available, PsySH can fork the process before executing code to provide better isolation.
How It Works
Fromsrc/ExecutionLoop/ProcessForker.php, the execution loop:
- Forks the process before code execution
- Executes user code in the child process
- Returns results to the parent via a pipe
- Preserves the REPL state in case of fatal errors
Benefits
- Fatal errors don’t crash the REPL
- Infinite loops can be interrupted
- Memory leaks are isolated
Disabling Process Forking
You can disable process forking:File System Operations
Reading Files
Writing Files
Directory Traversal
Readline Integration
PsySH uses the GNU Readline library (or alternatives) for command-line editing.Features Provided by Readline
- Line editing (Emacs or Vi keybindings)
- Command history
- Tab completion
- Multi-line editing
Readline Libraries
PsySH supports multiple readline implementations:- GNU Readline (via PHP
readlineextension) - Libedit (BSD systems)
- Hoa Console (pure PHP fallback)
Checking Readline Support
Custom Readline Configuration
Create~/.inputrc for Readline settings:
Platform-Specific Features
Unix/Linux/macOS
- Full signal handling with
pcntl - Terminal state management with
stty - Process forking for isolation
- Color output support
Windows
- Limited signal handling
- ANSI color support (Windows 10+)
- Console window detection via
mode con - No process forking (requires
pcntl)
Best Practices
Be careful with destructive commands
Be careful with destructive commands
Shell commands executed from PsySH have the same permissions as the user running PsySH. Be cautious with commands like
rm, mv, or database operations.Check command availability
Check command availability
Before executing system commands, verify they exist:
Handle command output
Handle command output
System commands may produce large output. Consider using file redirection:
Escape user input
Escape user input
When building shell commands with variables, use
escapeshellarg():