News for hackers. Stuff that matters.
Re:Path Isn't Real on Linux by mzajc on Tuesday April 29, 2025 @09:39PM (#43840285)
Fun fact: if you've ever had bash (or another shell) complain that a file doesn't exist, even though it's on $PATH, check if it's been cached by `hash`. If the file is moved elsewhere on $PATH and bash has the old path cached, you will get an ENOENT. The entire cache can be invalidated with `hash -r`.
Re:Path Isn't Real on Linux by JohnMakin on Tuesday April 29, 2025 @09:52PM (#43840352)
you just solved a bug I couldnt explain like 6 years ago
Re:Path Isn't Real on Linux by ninkendo on Tuesday April 29, 2025 @10:39PM (#43840663)
I think bash has an alias “rehash” that does the same as hash -r too. But zsh doesn’t have it, so “hash -r” has entered my muscle memory, as it works in both shells.

Edit: wrong shell, zsh has rehash, bash does not.

Re:Path Isn't Real on Linux by codesnik on Tuesday April 29, 2025 @10:47PM (#43840720)
but zsh has "rehash"? for as long as I remember.
Re:Path Isn't Real on Linux by ninkendo on Tuesday April 29, 2025 @10:55PM (#43840794)
Bah, you’re right! I got it backwards, it’s zsh that has rehash, bash does not. And hash -r works in both.

I guess I’ve been using zsh longer than I thought, because I learned about rehash first, then made the switch to hash -r later. I started using zsh 14 years ago, and bash 20+ years ago, so my brain assumed “I learned about rehash first” must have been back when I was using bash. zsh is still “that new thing” in my head.

Re:Path Isn't Real on Linux by macrocosmos on Tuesday April 29, 2025 @11:17PM (#43840916)
If there is something nice that one has and one does not have. zsh is the one that has it.
Re:Path Isn't Real on Linux by semiquaver on Wednesday April 30, 2025 @09:02AM (#43844646)
Counterpoint, /dev/udp pseudo-devices in bash.
Re:Path Isn't Real on Linux by NikkiA on Wednesday April 30, 2025 @08:46AM (#43844434)
the odd thing is, at some point I ended up with `hash -R` as muscle memory that I always type before I correct it to a lower case r, and I'm not sure why, I can't remember any shell that uses `-R`.
Re:Path Isn't Real on Linux by vlovich123 on Tuesday April 29, 2025 @10:12PM (#43840481)
Is this an old behavior? I would think ENOENT would invalidate the cache entry at least.
Re:Path Isn't Real on Linux by Aloisius on Tuesday April 29, 2025 @10:42PM (#43840682)
It's still a thing in bash 5.2.37.

It's just how bash works. If there's an entry in the session cache, it uses it. Since executable paths only get cached when you run a command successfully, this only happens when it gets moved from one directory in your PATH to another after you run it once, which isn't that common.

Setting PATH or calling hash -r will clear the session cache or one could run set +h which will disable it altogether.

Re:Path Isn't Real on Linux by Hackbraten on Wednesday April 30, 2025 @01:48AM (#43841657)
> this only happens when it gets moved from one directory in your PATH to another after you run it once

It also happens when you have two executables in different directories and then you delete the one with the higher priority. Happens regularly for me after I uninstall a Linux Homebrew package.

Re:Path Isn't Real on Linux by edoceo on Tuesday April 29, 2025 @11:57PM (#43841119)
Isn't cache invalidation one of the hard problems?
Re:Path Isn't Real on Linux by vlovich123 on Wednesday April 30, 2025 @10:52AM (#43846100)
Sure but not doing it on ENOENT suggests they’re just being completely lazy. Not to mention that they do have the tools (eg inotify watches) to proactively remove stale entries based on HD changes. Of course I’d be careful about the proactive one as it’s really easy to screw things up more (eg 100 bash instances all watching the same PATH directories might get expensive or forgetting to only do this for interactive mode connected to a TTY)
Re:Path Isn't Real on Linux by rendaw on Tuesday April 29, 2025 @11:52PM (#43841093)
Not working, as intended
Re:Path Isn't Real on Linux by mzajc on Tuesday April 29, 2025 @10:34PM (#43840632)
Retested with bash 5.2.37(1)-release from Debian testing, it still does this :(

Changing $PATH does wipe it at least.

Re:Path Isn't Real on Linux by bobbylarrybobby on Tuesday April 29, 2025 @10:07PM (#43840441)
Ah, so that's where sudo texhash -r comes from when installing a latex package!
Re:Path Isn't Real on Linux by noman-land on Tuesday April 29, 2025 @09:56PM (#43840390)
Wtf. TIL about hash.
Re:Path Isn't Real on Linux by ndsipa_pomu on Wednesday April 30, 2025 @03:23AM (#43842155)
Using "hash" is arguably the best way to determine if a command is available in a BASH script

e.g.

    hash java 2>/dev/null || printf "java command not available\n"
Re:Path Isn't Real on Linux by amiga386 on Wednesday April 30, 2025 @11:14AM (#43846439)
If you want to be compatible across all shells, use command -v. POSIX mandates it exists and has that returncode behaviour, whereas it doesn't mandate the hash, which or where command

    command -v java >/dev/null || echo 'no java command in path'
...and of course, if you're going to run the command anyway, and you know an invocation that does nothing and always exits with success, you can do that too. I like doing running "--version" or equivalent in CI systems, because it has the side effect of printing what actual versions were in use during the run.

    java -version || { echo >&2 'no java command in path'; exit 1; }
    git --version || { echo >&2 'no git command in path'; exit 1; }
    gcc --version || { echo >&2 'no gcc command in path'; exit 1; }
Re:Path Isn't Real on Linux by ndsipa_pomu on Wednesday April 30, 2025 @01:15PM (#43848160)
Yeah, if you're targetting POSIX shells, then "command -v" may be more reliable.

If you're targetting BASH, then "hash" is a builtin so maybe slightly quicker (not that it's likely to be an issue) and it caches the location of "java" or whatever you're looking for, so possibly marginally quicker when you do want to run the command.

Whilst running "java -version" may be useful in some scripts (my scripts often put the output into a debug function so it only runs it when I set LOG_LEVEL to a suitable value, but it writes output to a file and STDERR), you run into an issue of "polluting" STDOUT which then means that you're not going to be using your script in a pipeline without some tinkering (ironically you're putting the failure message into STDERR when you probably don't care as the script is exiting and hopefully breaking the pipeline). Also, it can take some research to figure out what invocation to use for a specific command, whereas the "hash" version can just be used with little thought.

By the way, I don't believe that ">&2" is POSIX compliant, but that's trivial to fix.

Re:Path Isn't Real on Linux by amiga386 on Wednesday April 30, 2025 @01:33PM (#43848376)
As far as I know, its POSIX compliant?

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V...

    The redirection operator:

    [n]>&word

    shall duplicate one output file descriptor from another, or shall close one. If word evaluates to one or more digits, the file descriptor denoted by n, or standard output if n is not specified, shall be made to be a copy of the file descriptor denoted by word
Re:Path Isn't Real on Linux by ndsipa_pomu on Wednesday April 30, 2025 @01:46PM (#43848542)
Fair enough - I usually just target BASH and don't worry about POSIX compliance.
Re:Path Isn't Real on Linux by DiggyJohnson on Tuesday April 29, 2025 @10:29PM (#43840589)
Holy shit
Re:Path Isn't Real on Linux by eternauta3k on Wednesday April 30, 2025 @01:36AM (#43841593)
The other typical cause is when an interpreter or library is compiled with the wrong libc version.
Re:Path Isn't Real on Linux by OptionOfT on Wednesday April 30, 2025 @03:02PM (#43849366)
Unsure of in which situation, but I've had situations where a script didn't have the right shebang, and as such I had to resort to `alias --save hash='#'` to make sure the script worked.