我试图理解即使可以做到,如何避免使用subshell?
这是唯一可以编写代码的方法吗?
我尝试使用括号{ ... }
,但它不会通过shellcheck并且不会运行。
is_running_interactively ()
# test if file descriptor 0 = standard input is connected to the terminal
{
[ -t 0 ]
}
is_tput_available ()
# check if tput coloring is available
{
command -v tput > /dev/null 2>&1 &&
tput bold > /dev/null 2>&1 &&
tput setaf 1 > /dev/null 2>&1
}
some_other_function ()
# so far unfinished function
{
# is this a subshell? if so, can I avoid it somehow?
( is_running_interactively && is_tput_available ) || # <-- HERE
{
printf '%b' "${2}"
return
}
...
}
答案 0 :(得分:2)
它是compound-list,是的,这些命令在子shell中运行。为了避免这种情况,请使用大括号而不是括号:
{ is_running_interactively && is_tput_available; } || ...