I looked for this in other questions but can't seem to find anything similar. Yet, this seems quite useful thing.

I want to set up a ZSH snippet/alias for my git add . so that it does three jobs in a chain:

  1. if path ./readme.md exists, run doctoc (globally installed CLI app from npm) on it. If it doesn't, do nothing and skip to next step.
  2. if path ./changelog.md exists, run a different local CLI installed script on it (let's say called changelogupd). If it doesn't, do nothing and skip to next step.
  3. no matter the previous outcomes, then run git add .

Now currently, I have two commands chained in .zshrc alias like this and it works:

path-exists ./readme.md && doctoc readme.md && git add . || git add .

But I want three: two path checks and conditional calls plus git add .

By the way. the path-exists is path-exists-cli, globally installed via npm.

Three command chain seems tricky, below is a nonsensical mash to show the rough thing I'm looking for:

path-exists ./readme.md && doctoc readme.md && git add . || path-exists ./changelog.md && changelogupd changelog.md && git add . || git add .

Should I try to cover all four cases using double pipes:


  1. readme present, no changelog, plus git add .
  2. readme not present, no changelog, plus git add .
  3. both readme and changelog present, plus git add .
  4. no readme but changelog present, plus git add .


or is there an easier way (or an npm CLI library or some secret terminal command)?

This is to be ran on a Mac terminal by the way. Thank you.

  • 1
    The idea of A || B is to run B only if A fails, which doesn't seem to be what you're looking for here, so any construction using || will be messy. Unless you have a really good reason to not do so, just write separate commands (with a newline or semicolon in between)
    – ilkkachu
    May 9, 2017 at 8:59

2 Answers 2


Just write a function:

my-git-add-.() {
  [ -e readme.md ] && doctoc readme.md
  [ -e changelog.md ] && changelogupd changelog.md
  git add .
  • super elegant solution, I didn't think about functions!
    – revelt
    May 19, 2017 at 10:48
path-exists ./readme.md    && { doctoc       readme.md;    :; } || \
path-exists ./changelog.md && { changelogupd changelog.md; :; }
git add .

You need to group the commands to achieve what you are wanting. The : statement has been added to supply a true ending when doctoc might fail.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .