Skip to content

/errors/find/xargs-unmatched-single-quote

xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · find (GNU findutils) 4.11.0updated
tool
find
exit code
1
versions
GNU findutils 4.x
os
linux, macos, windows

GNU xargs prints xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option when its input contains an apostrophe with no closing one. By default xargs splits input on blanks and interprets ', " and \ as quoting, so a file named it's.txt breaks it. Feed it NUL-separated names instead: find . -type f -print0 | xargs -0 COMMAND.

Reproduce it

bash
touch "it's.txt" notes.txt
find . -name '*.txt' | sort | xargs ls
echo "exit status: ${PIPESTATUS[2]}"

Output

xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option
exit status: 1

xargs stops at the bad line and exits with status 1. Names before it may already have been passed to the command, so the command can run on part of the input.

Fix: NUL separators

bash
touch "it's.txt" notes.txt "with space.txt"
find . -name '*.txt' -print0 | sort -z | xargs -0 ls -1

Output

./it's.txt
./notes.txt
./with space.txt

-print0 ends each name with a NUL byte, the one character that cannot appear in a file name, and -0 tells xargs to split on NUL and treat quotes, backslashes and spaces as ordinary characters.

Other options

ApproachCommandNotes
NUL-separatedfind . -print0 | xargs -0 CMDSafe for every file name
Newline-separated (GNU)xargs -d '\n' CMDSafe unless names contain newlines
No xargsfind . -exec CMD {} +Batches arguments like xargs
grep outputgrep -lZ PATTERN ... | xargs -0 CMD-Z (--null) ends each name with NUL
bash
touch "it's.txt" notes.txt
find . -name '*.txt' -exec ls -1 {} + | sort

Output

./it's.txt
./notes.txt

Sources