LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Adding a path variable (https://www.linuxquestions.org/questions/linux-newbie-8/adding-a-path-variable-947113/)

Taylrl 05-27-2012 10:20 AM

Adding a path variable
 
Hi,

I am trying to compile something and need to add a path variable to my .profile file I think. Underneath PATH="$HOME/bin:$PATH" I added
PATH="$home/rich/X/Development/Android/android-ndk-r8/ndk-build" which is where ndk-build is located. I want to be able to run this from any location by simply typing ndk-build. I have saved this but when I type ndk-build it tells me that the command is not found. Does anyone know where I am going wrong here?

Richard

colucix 05-27-2012 10:26 AM

Quote:

Originally Posted by Taylrl (Post 4688776)
I added PATH="$home/rich/X/Development/Android/android-ndk-r8/ndk-build" which is where ndk-build is located.

This is not the right way to go: the HOME variable is uppercase and maybe it includes also the rich directory, then you forgot to add the current PATH. It should be:
Code:

PATH=$HOME/X/Development/Android/android-ndk-r8/ndk-buil:${PATH}
export PATH

Regarding the proper location fro the statement, it depends from the type of invocation of the shell session. Please, take a look at section Bash Startup Files in the reference guide for details.

Taylrl 05-27-2012 02:02 PM

Thanks for your respone :-)

I changed the path to that posted above but it still isn't working. From what I read on the site I think it should be ok to add it into the .profile file. Any ideas why it still isnt working?

Cheers!

divyashree 05-27-2012 02:08 PM

Add this in your .profile:

Quote:

PATH="$HOME/rich/X/Development/Android/android-ndk-r8/ndk-build:$HOME/bin:$PATH"
export PATH
Then just logout and login again and try.

colucix 05-27-2012 02:35 PM

Quote:

Originally Posted by Taylrl (Post 4688915)
I think it should be ok to add it into the .profile file. Any ideas why it still isnt working?

Yes. I think .profile is not the right place. If you open a terminal in your graphical desktop environment you're running an interactive non-login shell and in that case (quoting from man bash):
Quote:

When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc then ~/.bashrc when those files exist and are readable.
In case you're running a login shell .profile could still not be the correct place:
Quote:

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.
This means that if there is a .bash_profile or a .bash_login file in your home directory, .profile will be ignored, since only the first encountered of these three is sourced.

Moreover, on many linux distributions the $HOME/.bash_profile contains some lines of code that check for the existence of $HOME/.bashrc and if it exists it's sourced, making a login shell read .bashrc as well.

In conclusion, the right place could be $HOME/.bashrc and/or $HOME/.bash_profile, but I would prefer the former for the reason explained above. On the other hand if you want to apply these changes/additions on a system basis (that is available to all users) you can put them in /etc/bash.bashrc or /etc/profile.

For the sake of completeness, what is a login shell? It is that one you start by entering your credentials, that is
  • the shell that runs beneath a graphical desktop environment,
  • the shell invoked from a virtual console (e.g. ALT-CTRL-F1, ALT-CTRL-F2 and so on),
  • the shell invoked remotely through ssh.
How do you recognize it is a login shell? By means of
Code:

$ echo $0
-bash

where the hyphen before bash denotes it is a login shell.

Taylrl 05-28-2012 02:45 PM

Thanks for the amazing and very thorough response! I always find it incredible that people on here are so helpful and I can't thank you enough. Once I understand enough I'm looking forward to being able to share my knowledge too.

Ok.

The trouble I am having now is that I cant change the bash.bashrc file as it is read only and the user is ROOT. When I try to log in using sudo it asks for a password and ........well .....I can't log in. Is there anyway that I can change the read/write privileges otherwise?

colucix 05-28-2012 03:21 PM

Quote:

Originally Posted by Taylrl (Post 4689594)
Thanks for the amazing and very thorough response! I always find it incredible that people on here are so helpful and I can't thank you enough.

You're welcome! :)
Quote:

The trouble I am having now is that I cant change the bash.bashrc file as it is read only and the user is ROOT. When I try to log in using sudo it asks for a password and ........well .....I can't log in. Is there anyway that I can change the read/write privileges otherwise
Well... if you don't have root privileges, maybe you don't really need to make the software available to all users. On the other hand if you are a sudoer (that is a user able to gain administrative privileges for some tasks through sudo) you have to insert your password when prompted.

BTW, just to clarify, is this a multi-user system or is it your personal machine? It makes the difference: if it is a multi-user system and the software installed in a non-conventional location must be shared among the users, then /etc/bash.bashrc can be the choice, or even better a newly created file in /etc/profile.d (this is another story, still not mentioned above). If it is your system or the software has to be used by... you, then your personal $HOME/.bashrc is the file to edit.

Taylrl 05-28-2012 03:27 PM

It is my personal machine. I'm not sure why I can't log into sudo as the password could only be a certain number of things and none of them work. Would it definitley be a password that I have chosen at somepoint?

I only really need to be able to edit the bash.bashrc file by any means really, or more accurately add a path variable by some means to something so that I can compile something from anywhere (That sounds very ambigous I know) :-)

colucix 05-28-2012 03:38 PM

If you're running Ubuntu, there is no root's password so you should be able to use your user's password, unless your system does an automatic login and you forgot it completely! ;) This problem can be addressed in one way or another and there are plenty of tutorials on how to reset a lost password (you can try the LQ search engine as well).

Regarding the original topic, since it is your personal machine I'd suggest to edit $HOME/.bashrc and leave /etc/bash.bashrc alone, unless you need to change it for other reasons not covered here.

Taylrl 05-29-2012 02:29 PM

Thanks again! Ok, I am around the password problem.

I have added
Code:

PATH="$HOME/rich/X/Development/Android/android-ndk-r8/ndk-build:$HOME/bin:$PATH"
export PATH

to my $HOME/.bashrc file but it still won't compile. Is there somewhere specific I should add it into the file as I have simply just added it on at the end?

Cheers!

Taylrl 05-30-2012 07:46 AM

Heyyy,

I am still stuck on this. Does anyone know if there is somewhere specific in the /.bashrc file that I need to add my PATH variable to? Also, does it need to point to the thing that to point to the directory that contains the file I want to link to or to the actual file itself.

Your help on this would be most appreciated as I can't move forwards at the moment.

Thanks!

pan64 05-30-2012 08:07 AM

can you tell me what is that file? you can simply execute: file $home/rich/X/Development/Android/android-ndk-r8/ndk-build
If it was a text file (probably a shell script) what is in the first line?
Have you checked the permissions on that file? What about the execute flag?

colucix 05-30-2012 11:56 AM

First open a new terminal and check if the environment variable PATH has been correctly setup:
Code:

echo $PATH
and post the output here. Then please, post the output of
Code:

ls -l $HOME/rich/X/Development/Android/android-ndk-r8/ndk-build
and the exact command and the error message you get.

suicidaleggroll 05-30-2012 12:15 PM

The PATH needs to point to the directory, not the file itself

If ndk-build is the only thing in that directory that you need, you could also just create an alias for it without changing your PATH at all, by adding the following into your $HOME/.bashrc
Code:

alias ndk-build="$HOME/rich/X/Development/Android/android-ndk-r8/ndk-build"

Also, are you sure $HOME/rich is where you mean to look? Assuming rich is the user, the location would be either:
/home/rich/
OR
$HOME

Since $HOME is an environment variable that points to the user's home directory.

Unless of course "rich" is a directory inside your user's home directory, in which case you have it right.

Taylrl 05-30-2012 03:11 PM

Thanks people! :-)

Quote:

Originally Posted by colucix (Post 4691315)
First open a new terminal and check if the environment variable PATH has been correctly setup:
Code:


echo $PATH

and post the output here.

What I got was

Code:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
Then when I typed

Quote:

Originally Posted by colucix (Post 4691315)
Code:


ls -l $HOME/rich/X/Development/Android/android-ndk-r8/ndk-build


This made me realise that my PATH wasn't pointing in the right place and I have now corrected it.

Thanks suicidaleggroll, you made me realise this too. I consequently changed the PATH to

Code:

PATH="$HOME/X/Development/Android/android-ndk-r8:$HOME/bin:$PATH"
export PATH

Which is now pointing to the directory that contains the file I want to execute, it didn't execute. I then tried adding

Code:

alias ndk-build="$HOME/rich/X/Development/Android/android-ndk-r8"
into $HOME/.bashrc instead (that is pointing at the directory and not the file) but that didn't seem to work either.

@pan64
It is a text file, a shell script. The first line reads;

Code:

#!/bin/sh
and the permissions are fine.

bamboozled.


All times are GMT -5. The time now is 07:47 AM.