bash与函数调用中的命令替换 [英] Command substitution in bash vs function calling
问题描述
我正在从一本书中编写一个shell脚本,并且有一个示例,其中我创建一个函数并稍后使用 $(function_name)
调用该函数.
I am writing a shell script from a book, and there is an example where I am creating a function and invoking that function later with $(function_name)
.
但是据我所知,我可以通过写一个函数名称来调用它.那么,使用名称和 $(function_name)
调用函数之间有什么区别?
But as far as I know, I can invoke a function by just writing its name.
So, what is the difference between calling a function with its name and with $(function_name)
?
推荐答案
$(...)
被称为命令替换.
简单地说, function_name
将完成输出.
Simply put, function_name
will print the output, done.
$(function_name)
函数的输出将成为要执行的新命令的一部分(通常作为参数).
$(function_name)
the output from the function will become a part (usually as an argument) as a new command to be executed.
echo "ls" #<--- assume this is the function
打开一个终端,然后尝试一下,您会更好地理解它:
Open a terminal, and give this a try, you will understand it better:
echo "ls"
和
$(echo "ls")
这篇关于bash与函数调用中的命令替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!