On Tue, Nov 17, 2026 at 03:33:17 +0100, Vincent Lefevre wrote:
> Using single quotes is not possible in these cases.
>
> $ printf "$?\n"
> 0
>
> but
>
> $ printf '$?\n'
> $?
The pedantically correct way to do this would be
printf '%s\n' "$?"
Or %d if you prefer, since the argument will be numeric with no
leading zeroes.
Yes, it's true that "$?\\n" is safe, because we're guaranteed that $?
will never contain any characters that have special meaning in a
printf format argument. But that's playing a dangerous game. In a
future update, someone may see that and decide to add some more
variable expansions to the first argument of printf, and then it
stops being safe.
Besides, if you're going to take shortcuts that are only made possible
by knowledge of content restrictions, just use
echo "$?"
Why only take *one* shortcut when you can take them all?