Command Chaining
noun
say it: “kuh-MAND CHAY-ning” /kəˈmænd ˈtʃeɪnɪŋ/
Joining commands on one line so each runs only if the one before it worked, only if it failed, or either way.
What is command chaining?
Command chaining joins commands on one line and decides from each one's exit code whether the next runs. && (read "and") runs the next only if the one before worked; || (read "or") only if it failed; ; runs it either way. cd folder && ls never lists the wrong folder, because ls runs only when the cd worked.
mkdir -p backup && cp edited/*.jpg backup/ && echo "Backup done"
grep -q IMG_0502 notes.txt || echo "IMG_0502 still to edit" >> notes.txt
In a sentence
- “With command chaining, cd exports && ls listed nothing when the folder's name was mistyped.”
- “Sam used command chaining with || to add a note only when grep didn't find the photo.”
Level: Apprentice · More Unix terms at this level
Updated