#!/usr/bin/env bash # # Run a command in each directory matching a pattern. # # Usage: x-foreach [args...] # x-foreach "ls -d */" "git status" # # Source: https://github.com/mvdan/dotfiles/blob/master/.bin/foreach set -euo pipefail if [ $# -lt 2 ]; then echo "Usage: $0 [args...]" exit 1 fi listing=$1 shift for dir in $(eval "$listing"); do ( echo "$dir" cd "$dir" || exit 1 "$@" ) done