On Tue, Apr 09, 2019 at 02:32:38PM +0700, Robert Elz wrote:
> The idea is basically just to do
>
> var=$( cmd )
>
> right? But without a fork. That's something that can be done today,
> no new syntax needed (bash might even do it sometimes, I don't know, the
> FreeBSD shell does.)
wooledg:~$ strace -o log bash -c 'x=$(echo hi)'
...
clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD,
child_tidptr=0x7f5166f16a10) = 19218
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
rt_sigaction(SIGCHLD, {sa_handler=0x562d61250410, sa_mask=[],
sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f5166f50940},
{sa_handler=0x562d61250410, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART,
sa_restorer=0x7f5166f50940}, 8) = 0
close(4) = 0
read(3, "hi\n", 128) = 3
read(3, "", 128) = 0
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=19218, si_uid=563,
si_status=0, si_utime=0, si_stime=0} ---
...
Bash always forks for $() as far as I'm aware, which is why bash 3.1
introduced printf -v var. That's the only way to get printf-formatted
output into a bash variable without using a temp file or a fork.