You can use the in-built AppleScript solution, as mentioned in [this thread](https://apple.stackexchange.com/questions/86005/shortcut-to-eject-all-external-hard-drives-but-not-mobilebackups/271261#271261) and [this page](http://osxdaily.com/2014/05/22/eject-all-mounted-volumes-command-line-mac/), by adding this to `~/.bash_profile`:

```bash
alias ejectall='osascript -e "tell application \"Finder\" to eject (every disk whose ejectable is true)"'
```

This will require you giving permission to Terminal to control Finder, or you will get this error:

```
execution error: Not authorised to send Apple events to Finder. (-1743)
```

If you want a pure `bash` solution, here is a function that you can call with `ejectall`. If you renamed your startup disk or have different Time Machine backups, you may need to edit the condition that filters out the drives.

```bash
ejectall() {
    total=0
    ejected=0
    
    for v in /Volumes/*; do
	if [[ $v != *"Macintosh HD" && $v != *"com.apple.TimeMachine"* ]]; then
	    echo "Ejecting $v..."
	    diskutil eject "$v" # The command to eject the volume
	    
	    if [ $? -eq 0 ]; then
		ejected=$(($ejected + 1))
	    fi
	    total=$(($total + 1))
	fi
    done
    
    if [ $total -eq 0 ]; then
	echo "No drives to eject"
    else
	msg="$ejected drive(s) ejected"
	failed=$(($total - $ejected))
	if [ $failed -gt 0 ]; then
	    msg="$msg, $failed drive(s) failed to eject"
	fi
	echo $msg
    fi
}
```

Both methods will also work for CDs.