Combine commands
This page was last updated on July 25, 2011.
These are some methods for combining more than one command in Bash. Note that although the examples show only two commands strung together, you can continue adding semi-colons, pipes, ampersands, etc., to add more commands.
| Syntax | Description |
command1 | command2 |
Execute command1. Use its output as input for command2. |
command1 || command2 |
Execute command1. If it fails, execute command2. |
command1 && command2 |
Execute command1. If it works, execute command2. |
command1; command2 |
Execute command1 and command2 regardless of whether either of them fail. |
command1 $(command2) |
Execute command2. Use its output as input for command1. |
command1 `command2 -options` |
Execute command2. Use its output as input for command1. Note: Those are backticks. |
Obligatory Happy Ending
And they all lived happily ever after. The end.
![]() |
Except where otherwise noted, content on http://mostlylinux.wordpress.com is Copyright © 2007 – 2011 by Little Girl, licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License, and offered free of charge without warranty of any kind, either expressed or implied. |

