简体   繁体   中英

How to change default directory for a command in Bash?

Firstly, I am new to Linux so excuse me if any terminology is wrong; I'll try to phrase the problem as competently as possible.

I have installed Ruby ( 2.4.0 ) via Linuxbrew. The ruby command works fine; it installed correctly. However, when I try to use the gem command (which Ruby should have installed) I receive this error: bash: /usr/bin/gem: No such file or directory

Now, because I installed this with Linuxbrew I know that this directory isn't correct. For example:

result of which gem : /home/me/.linuxbrew/bin/gem

result of which ruby : /home/me/.linuxbrew/bin/ruby

Therefore, it seems gem is installed but the gem command isn't linked to the correct path. I assume I need to direct the gem command to the path of which gem as opposed to /usr/bin/gem that bash is saying doesn't exist. How would I go about changing this? I tried in vain to change the bash_profile but I'm not sure what to do.

Again, excuse me if ruby and gem are not referred to as commands and if the problem isn't the "default directory" as stated in the title. I wasn't sure how to label it.

EDIT/TL;DR:

Basically, how can I make gem execute this: /home/me/.linuxbrew/bin/gem instead of looking for the program in /usr/bin/gem ?

Instead of running gem , run /home/me/.linuxbrew/bin/gem , ie type the full path name (followed by any arguments you may need).

If this becomes too tiresome, you could change your PATH. Prepend your bin directory with

PATH=$HOME/.linuxbrew/bin:$PATH

First, the reason you get the error /usr/bin/gem not found , is that earlier in the same shell session, the file used to be there. Bash will cache this to speed things up when running the same command many times. Running hash -r will clear this.

Editing PATH you seem to have managed, hence the which command gives the result it does.

To answer my own question-

As I had previously installed and uninstalled Ruby via apt-get instead of Linuxbrew in the same Terminal window, Bash was looking for gem in usr/bin as opposed to the path specified in my bash_profile to Linuxbrew.

Therefore, Stian's answer above with hash -r would also work, I am sure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM