I am somewhat curious about how it really works.
The original Lua 5.2.2 comes with the source but since it is not updated I downloaded Lua 5.3.1 myself and installed following the guide.
However I found that I only have access to 5.3.1 when I am the root user or using sudo, otherwise when typing lua -v it still shows 5.2.2
So I tried to uninstall 5.2.2 and install 5.3.1 again and nothing changed except I totally lose access to it when I am not root.
When typing “lua” it shows
/usr/bin/lua: No such file or directories
and the makefile installed it automatically into /usr/local and separated into several different directories. While there does have a directories named Lua among them, it seems contains nothing inside.
If I change its directory into /usr/bin/lua then it will be replaced by the lower version when updating the whole package, but I don’t think I should use sudo every time to use lua. Did anyone have the same problem? How did you deal with it?
Close the terminal window and open a new one, that should fix it.
The shell doesn’t re-evaluate all path directories whenever you run a command. It found lua in /usr/bin/ before, so it tries to (only) run it from there now.
That’s probably also the reason why your newer lua was not found before.
/usr/local/bin does come in the path before /usr/bin, so a lua installed there should be preferred over the system one in /usr/bin/.
And that’s the case for all users, not only root, unless you modified your user’s path manually.
In your case, you probably ran lua (the one in /usr/bin/) already, so the shell kept running it from /usr/bin/, although a newer one existed in /usr/local/bin/.
Running it via sudo started a new shell (as root), therefore the one in /usr/local/bin/ was found and used.
Yes.
As mentioned, bash remembers where it found a command, and doesn’t search for it in the whole path any more if it already knows (or believes to know) where to find it.
This is probably done to speed up things for often called commands.