The terminal can start bash subprogram through $bash command, wondering what is its use scenario?

  linux, question

Background

When learning Linux command, I learned that entering $bash command in bash can start a sub bash program.

My confusion

Since you can enter commands to operate in the terminal, why start another bash subroutine?
What is the motivation for starting bash subprogram in the terminal? What is the usage scenario?

Bash has interactive/non-interactive modes. Bash is just a well-known shell, and dash, zsh, fish, powershell. There is also a special environment for executing scripting languages. Their interpreters can also be interactive/non-interactive, such as python,nodejs. , you can enter the interaction mode of these interpreters from bash. of course, entering bash from bash is also one of them. You can also enter zsh from bash and python from zsh. So this is an unexpected and unexpected feature, and consistent applicability brings interesting effects.


Original answer:
I operated it once,$bashNo output.$(bash)Will enter sub bash interaction, and direct inputbashThe effect is the same.

This is an unexpected and unexpected feature. Looking at it alone, you may not find a meaningful application scenario, but the advantage of strong applicability is flexibility. What really works is that $ (). Its function is to open a subshell, and the command part enclosed by it will be executed first.

Bash can be followed by parameters and script files so that the results can be returned directly without entering interactive mode.
You can use variable assignment: var = $(command).
You can use pipes, command combinations: file $(which bash) and many wonderful combinations. Under normal circumstances$(command)And`command`The effect is the same.

Without parentheses, $ can represent several special variables, such as $PATH (enter env to view a bunch of environment variables), $# $0 $1 $@ used in script programming.