Skip to content

Get around xargs' lack of -d on macos

The problem

This is only a problem on MacOS, as xargs doesn't ship with -d on it.

Consider:

echo '1 1\n2 2\n3 3 3'
# 1 1
# 2 2
# 3 3 3

When trying to run a command on each line, eg. appending _updating to each line and printing it

echo '1 1\n2 2\n3 3 3' | xargs -n 1 -I {} echo "{}_updated"
# 1_updated
# 1_updated
# 2_updated
# 2_updated
# 3_updated
# 3_updated
# 3_updated

xargs splits each line based on space.

The Solution

Use tr '\n' '\0'. (reference)

echo '1 1\n2 2\n3 3 3' | tr '\n' '\0' | xargs -0 -n 1 -I {} echo "{}_updated"
# 1 1_updated
# 2 2_updated
# 3 3 3_updated