Skip to main content
added 78 characters in body
Source Link
Stéphane Chazelas
  • 584.5k
  • 96
  • 1.1k
  • 1.7k

Some shells (ksh93 and fish) do implement some optimisations there. In their $(type echo) ((type echo) in fish), they actually fake the writing of the output and the reading of it (FreeBSD sh also does it for single builtin invocations (like type here)). When a builtin's stdout is a command substitution, instead of writing the output, the shell just appends the would-be-output text to the command substitution result and there's no need for a fork.

Some shells (ksh93 and fish) do implement some optimisations there. In their $(type echo) ((type echo) in fish), they actually fake the writing of the output and the reading of it. When a builtin's stdout is a command substitution, instead of writing the output, the shell just appends the would-be-output text to the command substitution result and there's no need for a fork.

Some shells (ksh93 and fish) do implement some optimisations there. In their $(type echo) ((type echo) in fish), they actually fake the writing of the output and the reading of it (FreeBSD sh also does it for single builtin invocations (like type here)). When a builtin's stdout is a command substitution, instead of writing the output, the shell just appends the would-be-output text to the command substitution result and there's no need for a fork.

added 70 characters in body
Source Link
Stéphane Chazelas
  • 584.5k
  • 96
  • 1.1k
  • 1.7k
{
  type echo >&3
  echo 3>&- # close the writing end so the reader can see an eof
  IFS= read -r answer <&4
} 3>>|4

The most obvious ones are the standard read and getopts that do that by default (you do IFS= read -r var instead of var=$(line)). bash and zsh also have printf -v variable format args. zsh can do the same for its stat, strftime... builtins.

{
  type echo >&3
  IFS= read -r answer <&4
} 3>>|4

The most obvious ones are the standard read and getopts that do that by default. bash and zsh also have printf -v variable format args. zsh can do the same for its stat, strftime... builtins.

{
  type echo >&3
  echo 3>&- # close the writing end so the reader can see an eof
  IFS= read -r answer <&4
} 3>>|4

The most obvious ones are the standard read and getopts that do that by default (you do IFS= read -r var instead of var=$(line)). bash and zsh also have printf -v variable format args. zsh can do the same for its stat, strftime... builtins.

added 977 characters in body
Source Link
Stéphane Chazelas
  • 584.5k
  • 96
  • 1.1k
  • 1.7k

You'll find that in some shells that don't do that optimisation, a lot of the builtins can be called in such a way where instead of writing their result, they store it in a variable.

The most obvious ones are the standard read and getopts that do that by default. bash and zsh also have printf -v variable format args. zsh can do the same for its stat, strftime... builtins.

Some shells also make some information already available automatically in some special variables, like ksh's $SECONDS and $RANDOM found in a few other shells (and standard ones like $-, $# (equivalent to fish's (count $argv) for instance)).

In zsh, that's generalised to most of the shell's internal information so you almost never need to use command substitution on the output of a builtin. For instance, it's got associative arrays for the list of builtins, keywords, commands...

if (($+builtins[echo])); then
  echo echo is a builtin
fi

You'll find that in some shells that don't do that optimisation, a lot of the builtins can be called in such a way where instead of writing their result, they store it in a variable.

The most obvious ones are the standard read and getopts that do that by default. bash and zsh also have printf -v variable format args. zsh can do the same for its stat, strftime... builtins.

Some shells also make some information already available automatically in some special variables, like ksh's $SECONDS and $RANDOM found in a few other shells (and standard ones like $-, $# (equivalent to fish's (count $argv) for instance)).

In zsh, that's generalised to most of the shell's internal information so you almost never need to use command substitution on the output of a builtin. For instance, it's got associative arrays for the list of builtins, keywords, commands...

if (($+builtins[echo])); then
  echo echo is a builtin
fi
Source Link
Stéphane Chazelas
  • 584.5k
  • 96
  • 1.1k
  • 1.7k
Loading