# macOS: date: illegal option -- d / readlink: illegal option -- f / grep: invalid option -- P

**Code:** `illegal option on macOS (date -d, readlink -f, grep -P)` · **Category:** macOS · **Seen on:** macOS, Linux

macOS uses BSD versions of core tools, which lack many GNU flags that Linux scripts rely on.

## Meaning

Use the BSD equivalents (`date -v-1d`, `stat -f`, `realpath` from coreutils) or install GNU tools: `brew install coreutils gnu-sed grep findutils` (commands prefixed with `g`: `gdate`, `greadlink`, `ggrep`). For portable scripts avoid GNU-only flags or detect the OS.

## Common causes

- `date -d` (GNU) instead of `date -v` (BSD)
- `readlink -f`
- `grep -P` PCRE
- `xargs -r`, `sort -V`, `ls --color` differences
- Script copied from Linux

## Quick fix

1. `brew install coreutils` and use `gdate`, `greadlink -f`
2. Replace with portable alternatives (`python3 -c`, `perl`)
3. Put GNU tools first in PATH: `$(brew --prefix)/opt/coreutils/libexec/gnubin`

## Detailed fix by platform

### macOS

1. `date -v-1d +%F` (BSD) vs `date -d yesterday +%F` (GNU)

## How to diagnose

1. **Tool** — BSD or GNU?
2. **Flag** — GNU-only?
3. **Install** — coreutils?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/macos/illegal-option-gnu-flag-bsd-tool)
