That would be surprising. How would that work? `cat` is definitely not a bash built-in.
Other examples include `which`, `time`, `echo`, etc. They're usually promoted to built-ins when it's clear that's the intended command to run, but there are still analogs on the PATH if need-be - I'm not sure exactly how bash would detect a non-standard executable there to know when it's unsafe to promote to a built-in, though.
For example with echo:
$ bash -c 'echo hi'
hi
$ strace bash -c 'echo hi' | grep 'exec'
execve("/usr/bin/bash", ["bash", "-c", "echo hi"], 0x7fffe264aad0 /* 24 vars */) = 0
$ strace bash -c '$(which echo) hi' | grep 'exec'
execve("/usr/bin/bash", ["bash", "-c", "$(which echo) hi"], 0x7fffe36c6cd0 /* 24 vars */) = 0
execve("/usr/bin/echo", ["/usr/bin/echo", "hi"], 0x7fffe2e16ff0 /* 25 vars */) = 0
In fact for me, `cat` always gets inlined, even if I call it with its full path or with `which cat`.There are cases where the built-in and the actual executable differ - the most prominent case being `time`. The built-in does not have the command line arguments to print out things like pre-empted scheduling (forced context swiches).
$ time --help
--help: command not found
$ /usr/bin/time --help
Usage: /usr/bin/time [-apvV] [-f format] [-o file] [--append] [--verbose]
[--portability] [--format=format] [--output=file] [--version]
[--quiet] [--help] command [arg...] $ strace bash -c 'echo hi' | grep 'exec'
execve("/usr/bin/bash", ["bash", "-c", "echo hi"], 0x7fffe264aad0 /\* 24 vars */) = 0
Something seems off. strace(1) prints the trace to stderr not stdout, so you should need |& not | to catch it with grep. At least, that's how it works here.Also, cat does not appear to be inlined for me:
rascul@smarts:~> strace bash -c 'cat /etc/os-release' |& grep 'exec'
execve("/usr/bin/bash", ["bash", "-c", "cat /etc/os-release"], 0x7ffe7b5334e0 /* 88 vars */) = 0
execve("/usr/bin/cat", ["cat", "/etc/os-release"], 0x562c447076f0 /* 88 vars */) = 0*The output was copied 1:1 though.
The inlining is an implementation detail and I've seen it happen differently across different platforms.
Do you have more information on this? I would be interested to read more about it. As far as I was aware (and my knowledge is certainly not exhaustive), the only thing kinda related is as noted in the bash man page:
> The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).
strace -ff -e execve /bin/bash -c 'x="$(cat <<< "test")"'
... snip
[pid 311214] execve("/usr/bin/cat", ["cat"], 0x56280da76fc0 /* 3 vars */) = 0They are not comparable. I've never seen a shell inline cat, probably because it would make no sense given there are POSIX builtins to do exactly what cat does.
strace doesn't lie here, and I've seen it substitute cat in trivial cases quite often.
I'm genuinely curious to see an implementation but I suspect you've misunderstood something in the past and your statement was incorrect.
Your second strace is misleading, you didn't trace child processes.
$ strace bash -c '$(which echo) hi' 2>&1 | grep 'exec'
execve("/bin/bash", ["bash", "-c", "$(which echo) hi"], 0x7ffddfcdaee0 /* 59 vars */) = 0
execve("/bin/echo", ["/bin/echo", "hi"], 0x55eb565cb0b0 /* 59 vars */) = 0
$ strace -f bash -c '$(which echo) hi' 2>&1 | grep 'exec'
execve("/bin/bash", ["bash", "-c", "$(which echo) hi"], 0x7ffd09b3e228 /* 59 vars */) = 0
[pid 81493] execve("/usr/bin/which", ["which", "echo"], 0x5560d172e0b0 /* 59 vars */) = 0
execve("/bin/echo", ["/bin/echo", "hi"], 0x5560d172e0b0 /* 59 vars */) = 0There’s no runtime “promotion”. You can read about it all in bash(1).