Just discovered two useful features commonly available in shells:
1. printf "%q" 'thing!'
The %q sequence in the printf commands transforms an argument in a quoted version (see screenshot)
2. disown
This one is a builtin in bash and zsh, but not in dash. It disowns the last background job and, with that, closing the shell won't kill the disowned job. I'm using this for an opener script.

(I know printf isn't a shell builtin, but it is available in most systems, I guess)

@yohanandiamond

1. %q is GNU- and zsh-specific

It's in GNU /usr/bin/printf and bash/zsh builtins, but busybox printf and dash's builtin error out and it's not in the FreeBSD, OpenBSD, NetBSD, DragonFly BSD, macOS, or Solaris printf manpages.

2. printf(1) isn't just "available in most systems", POSIX requires it:

pubs.opengroup.org/onlinepubs/

3. printf actually is a builtin under busybox, dash, bash, and zsh… but only zsh makes `which` a builtin:

% which printf
printf: shell built-in command

Follow

@yohanandiamond

AFAICT, %q is specific to the CLI printf, so things like dash and BusyBox probably error out because their printf(1) just hands off to glibc's printf(3).

It wouldn't surprise me if the same implementation decision is also at play in all the non-GNU, non-Zsh stuff on the BSDs, macOS, and Solaris.

(After all, it *is* kind of nonsensical and counter-intuitive to have two separate implementations of printf formatting in the same userland rather than just putting %q into glibc.)

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.