|
|
|
Shell provides several ways to spawn and control processes. This covers subshells, command execution, and process management. |
|
|
Basic command execution: |
|
|
Commands in a subshell with (): |
|
|
Subshells don’t affect parent environment. |
|
|
Command groups with {} run in current shell: |
|
|
Capture command output with $(): |
|
|
Capture with backticks (older style, avoid): |
|
|
$() can be nested easily: |
|
|
Pipes connect stdout to stdin: |
|
|
Pipeline exit status is the last command’s status: |
|
|
Bash
Bash’s PIPESTATUS gives all exit codes: |
|
|
Process substitution (bash): |
|
|
POSIX alternative uses temp files or named pipes: |
|
|
Named pipe for process communication: |
|
|
Run command with timeout: |
|
|
Check if command exists before running: |
|
|
Run command conditionally: |
|
|
Compound conditionals: |
|
|
Command substitution in conditionals: |
|
|
Spawn and forget (nohup): |
|
|
Get PID of current script: |
|
|
Fork bomb protection - NEVER run this: :(){ :|:& };: # This is a fork bomb, DO NOT RUN |
|
|
Execute command from string: |
|
|
Be careful with eval - avoid if possible. |
|
|
exec replaces current process: |
|
|
xargs for batch processing: |
|
|
Parallel execution with xargs: |
|
|
Wait for specific process: |
|
|
Check if process is running: |
|
|
Source a script (run in current shell): |
|