AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1209630
Accepted
Gabriel Staples
Gabriel Staples
Asked: 2020-02-11 22:37:36 +0800 CST2020-02-11 22:37:36 +0800 CST 2020-02-11 22:37:36 +0800 CST

bash:在打开 gnome-terminal 选项卡时在 `bash -c` 命令中调用 ~/.bashrc 文件中定义的函数时出现“找不到命令”

  • 772

我正在尝试做的事情:

  1. 编写脚本以打开 3 个选项卡。
  2. cd进入每个选项卡中的不同文件夹(即:运行唯一命令)。
  3. 让每个标签都有一个唯一的标题

我想要这个脚本,所以我可以单击我的桌面上的脚本并让它打开终端,因为我想要我的日常开发环境。

描述:

我有这个脚本来尝试打开 3 个终端选项卡,其中包含要在选项卡中运行的独特命令:

open_tabs.sh

#!/bin/bash

gnome-terminal --tab -- bash -c "source $HOME/.bashrc && set-title hey; exec bash"
gnome-terminal --tab -- bash -c "cd ~; exec bash"
gnome-terminal --tab

当我使用 运行它时./open_tabs.sh,它会打开 3 个新标签,但不幸set-title的是无法设置标签标题!我在打开的选项卡中收到此错误:

bash: set-title: command not found

我已经set-title定义为这样的函数~/.bashrc。它的目的是在任何终端窗口的顶部设置标题字符串。当我手动使用它时它工作得很好。例如:set-title hey how are you?会放“嘿,你​​好吗?” 在我的终端窗口的顶部。

# From: https://unix.stackexchange.com/questions/177572/how-to-rename-terminal-tab-title-in-gnome-terminal/566383#566383
set-title() {
    # If the length of string stored in variable `PS1_BAK` is zero...
    # - See `man test` to know that `-z` means "the length of STRING is zero"
    if [[ -z "$PS1_BAK" ]]; then
        # Back up your current Bash Prompt String 1 (`PS1`) into a global backup variable `PS1_BAK`
        PS1_BAK=$PS1 
    fi

    # Set the title escape sequence string with this format: `\[\e]2;new title\a\]`
    # - See: https://wiki.archlinux.org/index.php/Bash/Prompt_customization#Customizing_the_terminal_window_title
    TITLE="\[\e]2;$@\a\]"
    # Now append the escaped title string to the end of your original `PS1` string (`PS1_BAK`), and set your
    # new `PS1` string to this new value
    PS1=${PS1_BAK}${TITLE}
}

我该如何解决!?我试过exporting 和sourceing 只是不知道我在这里做错了什么。

有关的:

  1. 打开带有多个选项卡的终端并执行为每个选项卡唯一修改 PS1 变量的应用程序
  2. https://unix.stackexchange.com/questions/177572/how-to-rename-terminal-tab-title-in-gnome-terminal/566383#566383
  3. 使用多个选项卡打开终端并执行应用程序<== 这是我真正想要解决gnome-terminal的问题,但现在不推荐使用--command( ) 选项!-e

    # Option “--command” is deprecated and might be removed in a later version of gnome-terminal.
    # Use “-- ” to terminate the options and put the command line to execute after it.
    
scripts gnome-terminal bash bashrc source
  • 1 1 个回答
  • 2025 Views

1 个回答

  • Voted
  1. Best Answer
    dessert
    2020-02-11T23:15:33+08:002020-02-11T23:15:33+08:00

    要让 bash~/.bashrc在启动时读取和执行,请将其作为交互式 shell 启动:

    gnome-terminal --tab -- bash -ic "set-title hey; exec bash"
    

    现在,为什么您在非交互式 shell 中获取文件的方法不起作用?我强烈认为你的~/.bashrc开头是这样的:

    # If not running interactively, don't do anything
    case $- in
        *i*) ;;
          *) return;;
    esac
    

    如果它所在的 shell 在它的任何地方都没有“i”,那么它可以return不做任何事情$-,即它是一个交互式 shell。$-是一个特殊的参数,man bash说:

    -扩展为调用时指定的当前选项标志,由set内置命令,或由 shell 本身设置的那些(例如-i选项)。

    • 3

相关问题

  • 如何每 5 秒运行一次脚本?

  • 如何将必须从其自己的目录中运行的程序添加到面板或主菜单?

  • 如何编写 shell 脚本来安装应用程序列表?

  • Mac OS X Automator 的替代品?

  • 备份 bash 脚本未压缩其 tarball

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve