LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-23-2020, 11:39 PM   #1
markus-n
LQ Newbie
 
Registered: Apr 2014
Distribution: Xubuntu, Arch
Posts: 20

Rep: Reputation: Disabled
Does bash remember where it found an executable ?


... and how do I reset it ?

Hi,

I'm currently experimenting with firejail. It's a tool that can put applications in a sandbox. It comes with lots of predefined profiles for standard applications like firefox (hence the name), geany, meld, ...

However, I'm not happy with some of the profiles, e.g. for geany (a text editor) and meld (a diff/merge tool). I'm still learning to configure firejail, but in the meantime I prefer to disable it for those applications. Disabling works by removing a symlink from /usr/local/bin. During my experiments, I'm removing and re-adding the symlink back and forth.
( For those who are interested in more detail, the symlink points to /usr/bin/firejail, which in turn checks with which name it was called, then reads the corresponding profile, and starts the application with the same name in a sandbox. )
And here comes the problem:
Once bash had found e.g. meld in /usr/local/bin (the symlink), it will only look for it it there. If I remove the symlink try to start meld, bash will complain about /usr/local/bin/meld not found. But the original is still in /usr/bin. And $PATH is /usr/local/bin:...:/usr/bin:... so I thought it should work.
At least
Code:
which meld
says
Code:
/usr/bin/meld

Does bash remember where it once found "meld" and refuse other locations from that moment on ?
And is there a way to make it forget that location (other than starting a new shell) ?
 
Old 10-23-2020, 11:56 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Bash will keep a history of the interactive session. See "man bash" for the built-in command history and the file ~/.bash_history in the section "SHELL BUILTIN COMMANDS" and the whole section "HISTORY"

Also look at the content of the environment variable $PATH. It is described in the manual page's sub section "Shell Variables".
 
Old 10-24-2020, 12:51 AM   #3
markus-n
LQ Newbie
 
Registered: Apr 2014
Distribution: Xubuntu, Arch
Posts: 20

Original Poster
Rep: Reputation: Disabled
I don't think it's about the history. I did not start meld including the path (like /usr/local/bin/meld) but only typed "meld", so the history only contains "meld".
To my undrstanding, typing "meld" (without any path) again should search the whole $PATH but it doesn't.

As I wrote in my original post, $PATH contains /usr/local/bin as well as /usr/bin.
 
Old 10-24-2020, 12:58 AM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
which uses $PATH
If which finds the executable, it’s in the PATH.
 
Old 10-24-2020, 01:02 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Ah. I understand now. I'm not sure of a solution, but I see that when an executable disappears or is moved, Bash keeps looking for it in the old location rather than re-checking $PATH for it. This might be a bug.
 
Old 10-24-2020, 01:04 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
I am guessing that the BASH_CMDS variable might be involved and that the shell built-in command hash might be of help.

Code:
hash -d meld

# or

hash -r

Last edited by Turbocapitalist; 10-24-2020 at 01:05 AM.
 
Old 10-24-2020, 04:17 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by Turbocapitalist View Post
Ah. I understand now. I'm not sure of a solution, but I see that when an executable disappears or is moved, Bash keeps looking for it in the old location rather than re-checking $PATH for it. This might be a bug.
I would say this is the usual cache problem: commands are cached to be able to find and execute them faster, but sometimes the cache will be outdated....
 
Old 10-24-2020, 05:32 AM   #8
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,572
Blog Entries: 19

Rep: Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451
Quote:
Originally Posted by markus-n View Post
Does bash remember where it found an executable ?... and how do I reset it ?
Short answer is yes. It's called hashing and it is designed to speed things up. But it's quite easy to switch it off. Just give the command
Code:
set +h
 
1 members found this post helpful.
Old 10-24-2020, 06:46 AM   #9
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546

The "hash" command is listed under the Bourne/POSIX built-in section, so is not Bash specific (nobody actually said it was, but I somehow made that assumption).

Quote:
hash [-r] [-p filename] [-dt] [name]

Each time hash is invoked, it remembers the full pathnames of the commands specified as name arguments, so they need not be searched for on subsequent invocations. The commands are found by searching through the directories listed in $PATH. Any previously-remembered pathname is discarded. The -p option inhibits the path search, and filename is used as the location of name. The -r option causes the shell to forget all remembered locations. The -d option causes the shell to forget the remembered location of each name. If the -t option is supplied, the full pathname to which each name corresponds is printed. If multiple name arguments are supplied with -t, the name is printed before the hashed full pathname. The -l option causes output to be displayed in a format that may be reused as input. If no arguments are given, or if only -l is supplied, information about remembered commands is printed. The return status is zero unless a name is not found or an invalid option is supplied.

In addition to the mentioned "set +h" there is also a "shopt checkhash" option that appears to be subtly different:

Quote:
-h

Locate and remember (hash) commands as they are looked up for execution. This option is enabled by default.
Quote:
checkhash

If this is set, Bash checks that a command found in the hash table exists before trying to execute it. If a hashed command no longer exists, a normal path search is performed.
From those descriptions, it sounds like the latter might maintain some performance benefits of avoiding path lookups, though I'm not sure there'd be a noticeable difference for most scripts, unless PATH happens to be really long and/or reference a slow filesystem.

 
1 members found this post helpful.
Old 10-24-2020, 01:49 PM   #10
markus-n
LQ Newbie
 
Registered: Apr 2014
Distribution: Xubuntu, Arch
Posts: 20

Original Poster
Rep: Reputation: Disabled
aaah ... hash ... OK :-)

Code:
hash -r
worked.

Thanks a lot. :-)
 
Old 10-29-2020, 11:36 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Quote:
From those descriptions, it sounds like the latter might maintain some performance benefits of avoiding path lookups, though I'm not sure there'd be a noticeable difference for most scripts, unless PATH happens to be really long and/or reference a slow filesystem.
or I'd think for loop that's run a million times?

Back in the day before even Perl was a normal thing, a lot of 'glue' was written using $<shell>, so on an old school prod box, that (hash) would have been beneficial in getting the work done noticeably faster.
 
Old 10-30-2020, 07:12 AM   #12
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546

That quote wasn't referring to three decades ago, and was contrasting "-h" and "checkhash", not the hash command.

Most shell scripts don't even loop a million times, never mind needing to deal with disappearing/moving commands during that looping.

I'd be curious about any current real-world script that does do such looping, but still has a reason to be written in shell...?

 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] No package 'x11' found No package 'xext' found No package 'xdamage' found No package 'xfixes' found No package 'x11-xcb' found Jigsaw Linux From Scratch 14 02-23-2021 08:35 PM
I have mint18 cinnamon installed on my Toshiba Satellite C850 and can remember the password but can't remember the username. stringybark Linux - Laptop and Netbook 11 12-29-2017 07:20 PM
[SOLVED] curious: bash: not found (although its there and executable) - maybe 64bit related? Handtuch Linux - Software 7 05-03-2012 10:29 AM
Why does "[ ] Remember me" does not work? archieval General 1 08-15-2010 10:14 PM
Running a Java executable class from another executable class LUB997 Programming 22 07-24-2005 04:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration