Geo*_*e D 2 command-line bash scripts environment-variables bashrc
我将 Ubuntu 14.10 与 bash shell 一起使用。
我下载了一个gradle发行版并将其移动到一个新目录。我想永久设置一个环境变量来指向bingradle 文件夹的子目录。
我已经编辑~/.bashrc并~./profile根据建议包含了 gradle 运行脚本的路径。然而,当我输入gradle(这是 bin 文件夹中脚本的名称)时,它不会运行。注销,重新启动和一切,但它仍然不起作用。
有什么我在这里想念的吗?
在末尾添加了这一行 .bashrc
导出 GRADLE=/home/sanctus/Documents/Development/gradle-2.2/bin
我的~/.profile文件的内容是这些:
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$HOME/sanctus/Documents/Development/gradle-2.2/bin"
fi
gradle="$HOME/sanctus/Documents/Development/gradle-2.2/bin"
export gradle
Run Code Online (Sandbox Code Playgroud)首先,你不想做gradle="..."。这只是创建了一个名为gradle并且无关紧要的变量(除非该变量以某种方式被使用,gradle但您没有这么说)。您要做的是将包含gradle可执行文件的目录添加到系统在尝试查找要运行的程序时搜索的目录列表中。这就是PATH变量的作用。
不覆盖 的现有内容也很重要PATH。因此,要添加foo到PATH,您可以:
PATH:"$PATH":foo
Run Code Online (Sandbox Code Playgroud)
并不是
PATH="foo"
Run Code Online (Sandbox Code Playgroud)
后者将删除所有内容PATH并将其替换为foo单独的。
因此,结合所有这些,您希望将以下行添加到您的~/.profile(或者~/.bash_profile如果存在,但不添加到您的~/.bashrc):
PATH:"$PATH:$HOME/sanctus/Documents/Development/gradle-2.2/bin"
Run Code Online (Sandbox Code Playgroud)
为什么~/.profile或~/.bash_profile不~/.bashrc?首先,因为这就是配置文件的用途。更重要的~/.bashrc是,每次启动新外壳时都会读取。因此,例如,每次打开一个新终端时。设置只需在该文件中设置一次的环境变量,每次打开终端时都会重置它们,这只是不必要的开销。
此外,设置~/.bashrc仅影响从命令行启动的程序。如果您使用 GUI(例如菜单项或.desktop文件)启动某些内容,则这些变量将不可用。
在包括 Ubuntu 在内的许多系统中,~/.profile当您以图形方式登录时读取。因此,该文件中设置的变量也可用于 GUI 程序。此外,最好设置这些变量,~/.profile因为该文件仅读取一次:在登录时。
此外,即使您将 shell 更改为 bash 以外的其他内容,这也将起作用,因为~/.profile许多最流行的 shell 都可以读取它。
重要提示:如果 a~/.bash_profile存在,它将被读取而不是~/.profile。因此,如果您确实有这样的文件,请改用该文件。我的建议是,如果您有 . ~/.bash_profile,只需将其删除并将其中的任何内容添加到您的标准~/.profile.