FIND YOUR NEXT FEW LINES

The snippet library.

Small, practical examples. Search by task or narrow things down below.

10 snippets foundClear filters
Bash ScriptsProcesses & environment

Change directory only for one operation

Bash Scripts: A subshell makes a temporary directory change without affecting later commands.

Bash Scripts✓ Tested
Bash ScriptsProcesses & environment

Collect two independent background results

Bash Scripts: Write each job’s output separately and wait for both before combining results.

Bash Scripts✓ Tested
Bash ScriptsProcesses & environment

Export a value to child processes

Bash Scripts: Only exported shell variables are inherited through the environment.

Bash Scripts✓ Tested
Bash ScriptsProcesses & environment

Handle a nonzero background-job exit

Bash Scripts: Wait reports a child’s exit code; put it in a conditional when failure is expected.

Bash Scripts✓ Tested
Bash ScriptsProcesses & environment

Keep loop state by avoiding a pipeline subshell

Bash Scripts: Redirect input into a while loop so updates to a counter remain in the current shell.

Bash Scripts✓ Tested
Bash ScriptsProcesses & environment

Preserve a failure status while an EXIT trap runs

Bash Scripts: Capture the status immediately on entry to the trap before running cleanup commands.

Bash Scripts✓ Tested
Bash ScriptsProcesses & environment

Remove a variable from one child’s environment

Bash Scripts: env -u removes an inherited value for a single command.

Bash Scripts✓ Tested
Bash ScriptsProcesses & environment

Set an environment value for one child command

Bash Scripts: A command-prefix assignment is visible to that command without overwriting the shell’s current variable.

Bash Scripts✓ Tested
Bash ScriptsProcesses & environment

Stop a child process created by the script

Bash Scripts: Send a signal only to the captured PID of the script’s own child and wait for it to exit.

Bash Scripts✓ Tested
Bash ScriptsProcesses & environment

Wait for a background job before reading its output

Bash Scripts: Save the job PID and wait explicitly before consuming files it produces.

Bash Scripts✓ Tested