如何在不键入完整路径的情况下运行脚本?

我想知道是否有办法在不键入完整路径的情况下运行程序/ shell脚本:

/opt/idea/bin/idea.sh 

你可以创建符号链接 。 在/usr/local/bin创建它。 您只需要运行命令:

 sudo ln -s /full/path/to/your/file /usr/local/bin/name_of_new_command 

之后你应该让你的file可执行:

 chmod +x /full/path/to/your/file 

现在,您应该可以在终端中随时运行name_of_new_command

请注意,这仅适用于Linux的家庭使用。

您可以将/opt/idea/bin添加到PATH变量:

 PATH=/opt/idea/bin:"$PATH" 

在此之后,您可以使用idea.sh运行它。

您可能想在~/.bashrc文件中添加此行。

你可以在~/.bashrc创建一个函数:

 some-name () { /path/to/your/file # or: #cd /path/to/your #./path } 

或者您可以创建alias

 alias some-name='/path/to/your/file' # or #alias some-name='cd /path/to/your/; ./file' 

在这两种情况下,您都可以通过调用来运行它:

 $ some-name 

如果文件不依赖于它的运行位置,请考虑将其添加到~/bin

 mkdir -p ~/bin cp /path/to/you/file ~/bin # or mv /path/to/you/file ~/bin # or ln -s /path/to/you/file ~/bin 

~/bin ,如果存在,会自动添加到$PATH 。 然后你直接调用file

 $ file 

(虽然名称选择不当,但请考虑称之为不太通用的东西。)

您可以使用以下命令创建启动器:

gnome-desktop-item-edit --create-new 。 我会打开这个窗口。

在此处输入图像描述

将其命名为您喜欢的名称,并在命令框中键入以下内容

sh -c '/opt/idea/bin/idea.sh'并保存。

现在,您可以使用新创建的启动器运行该文件

要么

您可以使用以下内容创建.desktop文件

 [Desktop Entry] Name= Exec=sh -c '/opt/idea/bin/idea.sh' Terminal=false Type=Application Icon='' 

现在用任何地方的.desktop扩展名保存它。

使用此命令chmod a+x 使其可执行

现在双击打开它。

我们可以使用bind命令来调用它来定义函数和添加热键。 打开~/.bashrc文件并将以下行添加到其中:

 # define function that opens your program in working directory Openprog(){ /your-Program/path/here } # bind hotkey to it () bind -x '"\e[24~":"Openprog"' 

现在,当您按F12时 ,您的程序将启动。

注意:确定转义码的快速方法:

打开终端并按Ctrl + V. 现在按您喜欢的键盘快捷键。 应该出现正确的转义码。 只需确保在添加快捷方式之前替换^[\e ,例如替换^[[24~ with \e[24~

我们也可以使用bash_aliases直接运行/opt/idea/bin/idea.sh文件

运行打开~/.bashrc文件,

 gedit ~/.bashrc 

删除行前的#并保存,以便行看起来像,

 if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi 

现在打开~/.bash_aliases文件,

 gedit ~/.bash_aliases 

在该文件中添加以下行并保存,

 alias idea='cd /opt/idea/bin && sudo ./idea.sh' 

最后获取~/.bashrc文件,

 source ~/.bashrc 

现在您可以直接运行/opt/idea/bin/idea.sh文件,

 idea 

除了其他好的答案之外,请考虑将符号链接到~/.local/bin并将此目录添加到PATH(例如,从.bashrc中)。 此方法不需要特殊权限(例如,与/usr/local/bin符号链接不同)。 这样,您可能会有一个标准的目录结构,而不会泛滥您的$ HOME。 在这些https://unix.stackexchange.com/问题上阅读更多相关信息:

/usr/bin direcotyr中创建它的软链接:

 ln -s /usr/bin/idea.sh /opt/idea/bin/idea.sh 

现在运行它:

 idea.sh 

或者你可以简单地使用

 nano ~/.bashrc 

并添加

 PATH=/full/path/to/file:"$PATH" 

在它结束时,然后保存并退出。 之后,您只需输入文件名即可。

我在这里和其他地方都遵循了所有的答案,所以很少有人没有提到你可能需要登出命令才能最终运作。

回顾一下,特别是对于Xubuntu ,但对于其他Debian / Ubuntu变体,我也写了这些简单的指令。

(在下面的说明中我们使用目录〜/ bin ,因为这自动地是这些操作系统查找命令的地方。请看这里

让您的命令工作的万无一失的说明:

 # Open Terminal with Ctrl + Alt + T (or your system's own shortcut) # You'll work in your home folder, no need to use sudo for any of this cd # Go to home directory mkdir -p bin # Create folder ~/bin if it doesn't exist # Be careful not to type /bin, that's the bin folder in system root: / sudo apt install nano # Skip this if you have Nano installed nano bin/yournewcommand # In Nano, type: printf "Your new command works! \n" # \n means line break # Ctrl+X To leave Nano # Ctrl+Y To save the unsaved file # Enter to confirm chmod +x bin/yournewcommand yournewcommand # If you just created the bin folder, this command might not yet work. # You must now log out and log back in to make the bin folder noticed (I think) yournewcommand # Now it works! (You can use Tab to autocomplete the command you're typing) # If you add a second file/command, it should work without logging out (at least in my tests)