繁体   English   中英

Bash脚本打开终端并cd到可变目录

[英]Bash script to open a terminal and cd to a variable directory

我想编写一个bash脚本,根据传递给它的参数在~/Documents/目录中打开一个gnome-terminal(例如./open.sh notes~/Documents/notes/打开该终端)

我将如何处理? 我知道gnome-terminal --working-directory=[directory]做类似的事情,但是它不接受字符串,所以我不知道在这种情况下是否可以使用它。

你可以试试看

#!/bin/bash
workDir="/home/user/Documents/$1"
if [ ! -d "$workDir" ]; then
    echo "directory not found, check your path"
else
    gnome-terminal --working-directory="$workDir"
fi

例;

./open.sh notes

这会在〜/ Documents / notes中打开一个新终端

您的脚本或函数可以简单地是:

open() {
    gnome-terminal --working-directory="Documents/$1"
}

看起来可以相对于用户的主目录指定工作目录。

open notes一样使用它在~/Documents/notes打开一个新终端。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM