0

I use Ubuntu 14.10 with bash shell.

I downloaded a gradle distribution and moved it to a new directory. I want to permanently set a environment variable to point to the bin sub-directory of the gradle folder.

I have edited ~/.bashrc and ~./profile according to recommendations to include the path to the gradle's running script. Yet when I type gradle (which is the name of the script in the bin folder) it does not run. Logged out, rebooted and everything but still it does not work.

Is there something I am missing here?

  1. added this line on the end of .bashrc

    export GRADLE=/home/sanctus/Documents/Development/gradle-2.2/bin

  2. My ~/.profile file's contents are these:

    # ~/.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
    
6

First of all, you don't want to do gradle="...". That simply creates a variable called gradle and is irrelevant (unless that variable is somehow used by gradle but you haven't said so). What you want to do is add the directory containing the gradle executable to the list of directories your system searches through when trying to find programs to run. This is what the PATH variable does.

It is also important not to overwrite the existing contents of PATH. So, to add foo to the PATH, you do:

PATH:"$PATH":foo

And not

PATH="foo"

The latter will remove everything from PATH and replace it with foo alone.

So, combining all this, you want to add the following lines to your ~/.profile (or ~/.bash_profile if that exists, but not to your ~/.bashrc):

PATH:"$PATH:$HOME/sanctus/Documents/Development/gradle-2.2/bin"

Why ~/.profile or ~/.bash_profile and not ~/.bashrc? First of all, because that's what profile is for. More importantly, ~/.bashrc is read each time you start a new shell. So, for example, each time you open a new terminal. Setting environmental variables that only need to be set once in that file, makes them be reset each time you open a terminal and that's just needless overhead.

Additionally, settings in ~/.bashrc only affect programs launched from the commandline. If you launch something using the GUI ( a menu entry or a .desktop file, for example) those variables will not be available there.

In many systems, including Ubuntu, ~/.profile is read when you log in graphically. Variables set in that file will therefore be available to GUI programs as well. Also, setting these variables in ~/.profile is preferred since that file is only read once: at login.

Additionally, this will work even if you change your shell to something other than bash, since ~/.profile is read by many of the most popular shells.

Important : If a ~/.bash_profile exists, that will be read instead of ~/.profile. So, if you do have such a file, use that one instead. My recommendation is, if you have a ~/.bash_profile, to simply delete it and add anything that was there to your standard ~/.profile.

0

First you should ensure that your script has execute permissions:

chmod +x $HOME/sanctus/Documents/Development/gradle-2.2/bin/gradle

You may need to use sudo to do the chmod above..

Now what you miss, when you call gradle you are using the environment variable PATH.

PATH is an environmental variable in Linux and other Unix-like operating systems that tells the shell which directories to search for executable files

Even though you add the path to $PATH you did it in the .profile while you have to do it in the .bashrc.

So why .bashrc and not .profile?

please take a look for my answer https://askubuntu.com/a/633820/150504

Now back to solution:

gedit .bashrc

Add the line:

 export PATH=$PATH:$HOME/bin:$HOME/sanctus/Documents/Development/gradle-2.2/bin

Now save and exit then source it

source .bashrc

Now test it.


Urgent Update:

PATH="$HOME/bin:$HOME/sanctus/Documents/Development/gradle-2.2/bin"

This would distort your PATH, and you can't use any command in /bin,/usr/bin,... since like that you just override your PATH and set it only to two dirs $HOME/bin and $HOME/sanctus/Documents/Development/gradle-2.2/bin. Remove this line from your .profile.

test if it affects your syste,

echo $PATH 

result should be(maybe slightly differs):

/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

If it's not you should resolve the problem. Add this to your .profile

$PATH=/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
  • No, .bashrc is not the right place to set environmental variables. That's precisely what ~/.profile is for. See my answer and here. – terdon Jun 13 '15 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.