Command chaining: Inline or Already running process

Command chaining allows you to run multiple commands sequentially in Linux. This is useful when you need to execute a series of related operations, such as downloading a file, extracting it, and then cleaning up. Command chaining can be done inline when starting commands or applied to processes that are already running.

Inline Command Chaining

Bash provides several operators for chaining commands together. The general syntax is:

<command1> <arguments1> <chaining operator> <command2> <arguments2> ...

Common Chaining Operators

  • ; (semicolon) Executes commands sequentially regardless of success or failure

  • & (ampersand) Runs the command in the background and immediately starts the next

  • | (pipe) Passes the output of the left command as input to the right command

  • && (AND) Executes the right command only if the left command succeeds

  • || (OR) Executes the right command only if the left command fails

You can also group commands using parentheses to execute them in subshells:

( <command1> && <command2> ) & ( <command3> && <command4> ) &

This executes two command pairs in parallel, where each pair runs sequentially within its subshell.

Command Chaining with Already Running Processes

The wait command allows you to chain commands after processes that are already running. It's a wrapper for the system call that waits for background processes to complete.

Basic Wait Usage

Here's an example of starting background processes and waiting for them to complete:

$ sleep 10 &
[1] 26176

$ sleep 10 &
[2] 26178  

$ sleep 10 &
[3] 26179

$ wait $(pidof sleep) && echo "Waited for 3 10-second sleeps"
[1]     Done          sleep 10
[2]-    Done          sleep 10  
[3]+    Done          sleep 10
Waited for 3 10-second sleeps

Advanced Process Identification

You can combine multiple commands to identify specific processes:

$ wait $(ps -ef | grep <process_name> | tr -s ' ' | cut -f 2 -d ' ') && echo "Process completed"

This command chain works as follows:

  • ps -ef Lists all processes with details

  • grep <process_name> Filters output to show only lines containing the process name

  • tr -s ' ' Removes duplicate spaces from the output

  • cut -f 2 -d ' ' Extracts the second column (PID) using space as delimiter

Limitations of the Wait Command

The wait command has several limitations:

  • Shell scope Can only wait for processes started in the same shell session

  • Completed processes Cannot reliably return exit codes for already completed processes

  • Signal handling Cannot distinguish between normal termination and signal termination for exit codes above 128

For processes started in other shells or by other users, you can use a polling loop:

while ( pidof -q <command_to_wait> ); do sleep 1; done && <command_to_execute_after>

This loop checks every second if the process is still running and executes the next command when it completes.

Conclusion

Command chaining provides powerful ways to sequence operations both inline and with running processes. Inline chaining uses operators like &&, ||, and ; for immediate execution control, while the wait command enables coordination with background processes. These techniques are essential for creating robust shell scripts and automated workflows.

Updated on: 2026-03-17T09:01:38+05:30

477 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements