LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   edit to .bashrc makes other directories executable, not just ~/bin (https://www.linuxquestions.org/questions/slackware-14/edit-to-bashrc-makes-other-directories-executable-not-just-%7E-bin-4175693200/)

amikoyan 04-06-2021 05:50 AM

edit to .bashrc makes other directories executable, not just ~/bin
 
I added this to my .bashrc

Code:

. /etc/profile
PATH=$PATH:~/bin

expecting it would just cause ~/bin to allow files to be executed. However, it has allowed me to execute files in other directories located in the home directory.

I also tried adding this to .bashrc (not at the same time):
Code:

export PATH=~/bin:"$PATH"
with the same result.

I just want ~/bin to be able to allow files to be executed

TenTenths 04-06-2021 05:59 AM

Execute permissions are nothing to do with the PATH

Execute permissions are based on whether or not the individual file has execute for owner/group/world

pan64 04-06-2021 06:00 AM

you need to check the meaning of PATH: https://en.wikipedia.org/wiki/PATH_(variable)
That is just a search path, it does not allow or deny execution.

So would be nice to explain better what do you need, probably an example ....

amikoyan 04-06-2021 06:09 AM

Sorry, my fault, I am not quite sure what I am asking.

I only want the script to run when I type its filename when it is located in ~/bin.

If the script is anywhere else I would expect to have to type ./filename.

With the text added to .bashrc I just type the filename and the script runs from any directory in ~/.

I hope I have explained my intentions more clearly

pan64 04-06-2021 06:34 AM

that's why you should give us an example, what did you try exactly....

amikoyan 04-06-2021 07:01 AM

There is a bash script located in ~/test. It is called hello_world

Before editing .bashrc as shown in the posts above
Code:

mik@linuxbox:~/test$ hello_world
bash: hello_world: command not found

Which is expected

After editing .bashrc as shown in the posts above

Code:

mik@linuxbox:~/test$ hello_world
Hello World!

Which is not expected, as I want only scripts located in ~/bin to do this.

Why is the shell searching my ~/test directory for executable files when I only want it to search ~/bin for executable files?

pan64 04-06-2021 07:04 AM

how is your PATH variable set ? (can you post the result of: echo $PATH)

amikoyan 04-06-2021 07:06 AM

Code:

echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/games:/sbin:/usr/sbin:/usr/lib64/kde4/libexec:/usr/lib64/qt/bin:/usr/share/texmf/bin:/home/mik/bin


Alien Bob 04-06-2021 07:11 AM

My guess is that you have a copy of that file in ~/bin/hello_world and that one gets executed.

amikoyan 04-06-2021 07:22 AM

Alien Bob, you are correct, there is a copy of hello_world in ~/bin.

I have deleted it. But then when I cd to ~/test this is what happens:

Code:

mik@linuxbox:~/test$ ls
c-scripts/  hello_world*  hello_world~*
mik@linuxbox:~/test$ hello_world
bash: /home/mik/bin/hello_world: No such file or directory


pan64 04-06-2021 07:28 AM

you can use the command which to check where it was actually found.
You might want to check the thread as solved.

Petri Kaukasoina 04-06-2021 07:32 AM

Code:

help hash
hash -t hello_world
hash -r


Alien Bob 04-06-2021 07:32 AM

Quote:

Originally Posted by amikoyan (Post 6237718)
Alien Bob, you are correct, there is a copy of hello_world in ~/bin.

I have deleted it. But then when I cd to ~/test this is what happens:

Code:

mik@linuxbox:~/test$ ls
c-scripts/  hello_world*  hello_world~*
mik@linuxbox:~/test$ hello_world
bash: /home/mik/bin/hello_world: No such file or directory


This is a Bash behavior, it remembers (or 'hashes) the path from where it started your programs.
Try
Code:

$ type hello_world
You should get something like:
Code:

hello_world is hashed (/home/mik/bin/hello_world)
To remove this hash so that Bash no longer remembers the path where it found the program:
Code:

$ hash -d hello_world
$ hello_world
bash: /home/mik/bin/hello_world: No such file or directory


amikoyan 04-06-2021 07:36 AM

Thank you to all who helped, especially for the explanation about hashing, Alien Bob.

Also to Petri Kaukasoina, who also mentioned hash.

bassmadrigal 04-06-2021 12:33 PM

Quote:

Originally Posted by Alien Bob (Post 6237725)
This is a Bash behavior, it remembers (or 'hashes) the path from where it started your programs.
Try
Code:

$ type hello_world
You should get something like:
Code:

hello_world is hashed (/home/mik/bin/hello_world)
To remove this hash so that Bash no longer remembers the path where it found the program:
Code:

$ hash -d hello_world
$ hello_world
bash: /home/mik/bin/hello_world: No such file or directory


I've been using Linux for close to 20 years and I never knew about this! Thanks for teaching me something new! For those interested in learning more, see this StackExchange answer.


All times are GMT -5. The time now is 06:36 PM.