How to remove the existing environment variable in Linux? - ruby-on-rails

This is my machine path :
~$ echo $PATH
/home/sam/.rvm/gems/ruby-2.1.2/bin:/home/sam/.rvm/gems/ruby-2.1.2#global/bin:/home/sam/.rvm/rubies/ruby-2.1.2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/sams/.rvm/bin:/usr/local/bin
Here, I want to remove all the paths related to rvm
I removed all the above lines from .bashrc and .bash_profile but still it exists in the GEM_PATH where else it is erased from the common PATH

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
check:
echo $PATH
you can add the export code inside .bashrc

The first place to look for alternations in the $PATH variable would be your .bashrc file. Look for something like:
export PATH=/your/homefolder/.rvm:$PATH
Another thing worth asking, do you have this behaviour on startup of the system or is it just in one local shell window? If it's only in the current shell session (and not in the .bashrc or something similar), it will not stick/be permanent.

You can manually overwrite your PATH through exporting it again, but that only affects the current session.
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
If you want the change to last, you have to find where the rvm parts are added to the PATH variable. That's probably at the end of the ~/.bashrc (or ~/.profile, or ~/.bash_profile, or /etc/profile, .. it depends on how you installed rvm) file. You can safely remove those lines that add rvm to the path.
In case you also want to uninstall rvm (which I suspect if you go the second route), do a
rvm implode

To remove Rvm, run
$ rvm implode
Are you SURE you wish for rvm to implode?
This will recursively remove /Users/gaurish/.rvm and other rvm traces?
(anything other than 'yes' will cancel) > yes
Removing rvm-shipped binaries (rvm-prompt, rvm, rvm-sudo rvm-shell and rvm-auto-ruby)
Removing rvm wrappers in /Users/gaurish/.rvm/bin
Hai! Removing /Users/gaurish/.rvm
This will remove RVM & its entire from your path.
ANd if you want to keep rvm or have already removed it manually. Now, just want to get rid of the rvm related entries from PATH. find this line which is responsible for adding rvm to your path:
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
using grep
$ grep -nr 'PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting' ~
Now, you will have the list of files along with line number that contain this line. you can ignore any entries in history. Mainly focus on entires in ~/.bashrc, ~/.profile, ~/.zshrc and other config files. And remove each of those

Related

where the $PATH is created in ubuntu (16.04) and how to change it

when checking my $PATH on ubuntu (16.04)
I get a long list of directories, few of which even do not exist in my file
system, and some of them I just don't need:
echo $PATH
.../usr/games:/usr/local/games:/snap/bin
where they are created and how can I remove them?
I wnant to control the creation of the $PATH, rather than
correct it later by the tricks described in
https://unix.stackexchange.com/questions/108873/removing-a-directory-from-path
Some typical places where $PATH can be set when starting up a bash shell on Ubuntu include:
/etc/profile
~/.profile
~/.bashrc
where ~ represents your home directory.
Also look at any scripts called by those scripts.
There may be other things that get called when starting up a bash shell, depending on various conditions. For details, take a look at the INVOCATION section from the command:
$ man bash
See this answer from askubuntu.com to edit the path either using a text editor or the command line.
I found the answer to your question today. The path you want to edit is in /etc/environment.

Modularizing and distributing bash script via Homebrew

Context
I have some functions defined in my ~/.bashrc which I'd like to turn into a Homebrew package. Currently, these functions act as custom commands on my command line:
# .bashrc
function foo() {
# do something interesting
}
# at terminal
$ foo
# => does the interesting thing
Approach
I've created a homebrew formula using brew create. My current approach is as follows:
Move the function definitions into a separate file, script, within a directory, brew-script
Make brew-script downloadable as a tarball, brew-script.tar.gz
Have my brew formula append text to the end of ~/.bash_profile to include script when terminal session starts
Concerns
Is modifying .bash_profile in a brew formula bad practice? (eg. when uninstalling with brew uninstall script, brew should somehow remove the text that was appended to .bash_profile... Parsing .bash_profile doesn't seem very fun.)
Is there a convention already established for including functions in bash scripts so that they are available from the command line?
Is it common to simply ask the user to add some text to their .bash_profile or .bashrc?
Desired result
Should be able to install cleanly with brew and then run foo as a command:
$ brew install script
$ foo
# => does the interesting thing
(Assume the brew formula is already installed locally. I'll worry about auditing and pushing the formula to homebrew later)
Refer https://github.com/Homebrew/homebrew/issues/50232 and https://github.com/Homebrew/homebrew/issues/50231.
I have a script that safely‡ modifies ~/.bash_profile as part of a homebrew install process. https://github.com/paul-hammant/homebrew-tap/blob/master/switchjdk.rb
‡ allegedly
Without using homebrew:
to put your bash scripts in some file such as bashrc or any other name works, then put the following line:
source "path/to/brew-script/script"
somewhere in your bash profile.
Then you just have to make sure you refresh or reload your bash profile by running . ~/.bash_profile or source ~/.bash_profile.
How homebrew installs work:
When you installed homebrew it added a line to your bash_profile that modifies your $PATH variable to include the path to the homebrew install repo, so that whenever brew installs something it becomes findable through your PATH.
If you use brew create you must have your script uploaded somewhere on the internet, because the argument brew install takes is a URL. I.e if I create my script at my_bash_function.tar.gz then I would do
brew create http://web.mit.edu/dianah13/www/my_bash_function.tar.gz
It also templates a pull request to include your package in homebrew's main repo.

Bash-profile error and Rails

I tried to edit my bash_profile earlier. I think I put a space after the '=' and then I couldn't use any command line tools. I've now managed to get them back, although my terminal now says I don't have rails installed. I sudo install it, but it fails because it asks me to replace the rake gem with the rake executable. I say no to that request. I have been using rails to follow a tutorial. Unless it has been wiped, I have it. There must be something wrong with the path, but I don't know what the bash_profile should be. It is currently:
PATH=/usr/local/rvm/bin:$PATH
PATH=/Users/me/.rvm/gems/ruby-2.0.0-p247/bin
PATH=Users/me/.rvm/gems/ruby-1.9.3-p448/bin
PATH="/Applications/Postgres.app/Contents/MacOS/bin:$PATH"
"$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into $
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
I don't know how to go about fixing this.Thanks in advance for any help you might be able to offer.
These two lines replace the entire PATH with a single directory:
PATH=/Users/me/.rvm/gems/ruby-2.0.0-p247/bin
PATH=Users/me/.rvm/gems/ruby-1.9.3-p448/bin
There is now absolutely nothing in your command search path except "Users/me/.rvm/gems/ruby-1.9.3-p448/bin", with missing / so that it only works if you're in the root directory, no less.
Then you add some stuff to the PATH without replacing what's there, which is fine, but then you do this:
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
Which completely undoes all of that and gives you just the above literal path.
You generally don't want to assign to PATH without a $PATH somewhere on the right hand side.

RVM not found, after installing RVM

I've found a couple similar posts regarding this same problem, but none of the solutions seem to apply, here.
On a fresh Ubuntu 10.10 install, I follow the instructions for installing RVM:
$ bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )
Then I create .bash_profile and add the following line:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
I restart the terminal and check RVM:
$ type rvm | head -1
-bash: type: rvm: not found
As the RVM installation guide explains to do so, I replaced the first line (below) in .bashrc with the second one, then indented everything in the rest of the file and added a fi.
[ -z "$PS1" ] && return # original
if [[ -n "$PS1" ]]; then # replaced with this
Restarted terminal and still, no luck.
Then, I removed the line I added to .bash_profile in the beginning and added it to .bashrc, even though that isn't what the guide said to do. Still, no luck. I also entered it directly on the command line, with no change in behavior. When I run .rvm from ~/.rvm/bin/rvm it complains that there is no such file or directory as /.rvm/scripts/rvm and that the command was not fund.
Of course, there isn't any such "scripts" directory inside of ./rvm, either -- so I'm not sure why it's looking for one? The only directories inside of .rvm are
archives
bin
config
gems
gemsets
log
man
rubies
src
tmp
user
The only thing I've found while googling for answers are other people complaining of similar problems and people telling them to add the instructed line to .bash_profile (which I obviously already did). At this point, I have nothing more to go on and am at an impasse.
Regards.
Resolution:
As Andrew Marshall advised in his comments, below, I did an 'rm -rf .rvm' and reinstalled rvm. I had actually attempted this two times before posting here, with the same results every time. No odd messages in the install log, but no /scripts/ directory, either. Just so I could say I had, I did it a third time at Andrew's urging. This time, I checked and the /scripts/ directory existed. Running 'type rvm | head -1' confirmed it as a 'function' and I can now move on.
Make sure that you restart a session after reinstalling, so that rvm is in your path.
You can try to logout/login.
You can also open your shell as a login shell. Under ubuntu 12.04:
Open a terminal
Edit > Profile Preferences
Under tab Title and Command, check run Command as a login shell
Open new terminal (ctrl+alt+t) and type rvm
If there's no scripts directory inside .rvm, it would seem that RVM failed to successfully complete installation. Delete the .rvm directory, try reinstalling, and look at the installation output closely to see if it's complaining about anything.

How to permanently change sudo's $PATH variable (Ubuntu 9.x)

I want add some directory to the $PATH when running sudo, this is a (semi) permanent requirement, not something that needs to be added to the scripts themselves. I notice that Django has managed to do it, (my $PATH when running sudo is "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin:/django/django-trunk/django/bin") - so how did it do that?
This is the line in the sudoers file that resets:
Defaults env_reset
You can work around this by adding PATH to env_keeps or by adding this line:
Defaults env_keep = "PATH"
EDIT: meder, you do not disable env_reset, you simply bypass the path reset
Or you can remove the offending env_reset line.
Even better though, you can declare a secure_path that will replace PATH when sudo is run:
Defaults secure_path="/bin:/usr/bin"
That way you can control what specific directories to include in the path.
I think this should work out if you save it in /root/.bashrc:
export PATH=/www/foo:$PATH
I forget if it's PATH or PYTHONPATH and if it actually matters, this is based on my user's .bashrc:
export PYTHONPATH=/www/django:$PYTHONPATH
You can set the variable in /etc/environment, and then use "sudo -i" to run the script (works in ubuntu 10.10).

Resources