📅  最后修改于: 2023-12-03 15:13:01.957000             🧑  作者: Mango
In Bash, there are two ways to execute commands within a command substitution context: $()
and ()
.
The $(command)
syntax is known as command substitution. It allows the output of a command to be substituted as an argument to another command or assigned to a variable.
For example,
echo "The current working directory is $(pwd)."
This will output "The current working directory is /path/to/current/directory."
The command
in $(command)
can also be written inside a subshell using ()
instead of $()
.
output=$(echo "hello, world")
echo "$output"
This is equivalent to:
output=`echo "hello, world"`
echo "$output"
The (
and )
within the subshell run the commands inside the subshell in a separate environment from the outer shell.
Use $()
when you want to capture the output of a command and use it in another command or assign it to a variable.
Use ()
when you want to group commands together and run them in a separate environment as a unit.
Understanding the differences between $()
and ()
can help simplify writing bash scripts and executing commands in the terminal. It is a powerful feature of Bash and can be a time saver for developers.