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

Gab*_*les 5 bash scripts gnome-terminal bashrc source

我正在尝试做的事情:

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

我想要编写此脚本,以便我可以单击桌面上的脚本并让它打开终端,就像我想要的日常开发环境一样。

\n\n

描述:

\n\n

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

\n\n

open_tabs.sh

\n\n\n\n
#!/bin/bash\n\ngnome-terminal --tab -- bash -c "source $HOME/.bashrc && set-title hey; exec bash"\ngnome-terminal --tab -- bash -c "cd ~; exec bash"\ngnome-terminal --tab\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
bash: set-title: command not found\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
# From: https://unix.stackexchange.com/questions/177572/how-to-rename-terminal-tab-title-in-gnome-terminal/566383#566383\nset-title() {\n    # If the length of string stored in variable `PS1_BAK` is zero...\n    # - See `man test` to know that `-z` means "the length of STRING is zero"\n    if [[ -z "$PS1_BAK" ]]; then\n        # Back up your current Bash Prompt String 1 (`PS1`) into a global backup variable `PS1_BAK`\n        PS1_BAK=$PS1 \n    fi\n\n    # Set the title escape sequence string with this format: `\\[\\e]2;new title\\a\\]`\n    # - See: https://wiki.archlinux.org/index.php/Bash/Prompt_customization#Customizing_the_terminal_window_title\n    TITLE="\\[\\e]2;$@\\a\\]"\n    # Now append the escaped title string to the end of your original `PS1` string (`PS1_BAK`), and set your\n    # new `PS1` string to this new value\n    PS1=${PS1_BAK}${TITLE}\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

有关的:

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

    \n\n
    # Option \xe2\x80\x9c--command\xe2\x80\x9d is deprecated and might be removed in a later version of gnome-terminal.\n# Use \xe2\x80\x9c-- \xe2\x80\x9d to terminate the options and put the command line to execute after it.\n
    Run Code Online (Sandbox Code Playgroud)
  6. \n
\n

des*_*ert 3

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

\n\n
gnome-terminal --tab -- bash -ic "set-title hey; exec bash"\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
gnome-terminal --tab -- bash -ic "set-title hey; exec bash"\n
Run Code Online (Sandbox Code Playgroud)\n\n

return如果它所在的 shell 在其任何位置没有 \xe2\x80\x9ci\xe2\x80\x9d $-,即 \xe2\x80\x99s 是一个交互式 shell,那么它就可以不执行任何操作。$-是一个特殊的参数,man bash表示:

\n\n
\n

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

\n
\n