linux - 在 bash 中组合函数

标签 linux bash terminal

我在我的~/.bash_aliases 文件中写了两个函数;一个返回当前工作文件夹(目录路径中最后一个 / 之后的所有内容),一个设置终端选项卡:

function set-location { printf "\e]2;$1\a"; }
function get-location {
    local location=${PWD##*/}
    echo "$location"
}

假设我在目录 james/foo/bar 中。结合这两个功能,我希望终端窗口设置为 bar。但是,我不知道如何有效地组合它们。我尝试了以下方法但无济于事,尽管我只是在猜测此时会发生什么:

set-location get-location # terminal title: get-location
set-location $get-location # terminal title: -location
set-location ${get-location} # terminal title: location
set-location "${get-location}" # terminal title: location
get-location | set-location # terminal title: Terminal

如何将这两个函数组合在一行中,以便将位置设置为 get-location 的结果?

最佳答案

使用命令替换

set-location "$(get-location)"

关于linux - 在 bash 中组合函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52150449/

相关文章:

linux - Bash 将变量作为带引号的参数传递

linux - 如何回显包含双引号的文本

terminal - 从 PuTTY 终端打开 Sublime Text 编辑器

linux - CRON 每 45 分钟运行一次脚本,但不是同时运行

c - 关于锁定 fread/fwrite 并从不同进程调用

linux - 基于 GeoIP 的 Apache 重写规则

linux - vim setwidth 在 emacs 中等效

linux - 将密码安全地传递给另一个程序(单独的 shell/dbus)

bash - 如何对成批文件运行命令?

linux - 随机文件夹名称在终端提示之前