John
John

Reputation: 612

SAFELY removing duplicates from $PATH, Ubuntu 16.04

I am a new programmer with less than 7 months' experience. I want to clean up my $PATH because it's filled with numerous duplicates.

E.g.

echo $PATH

Yields this mess

/home/john/.rvm/gems/ruby-2.4.1/bin:/home/john/.rvm/gems/ruby-2.4.1@global/bin:/home/john/.rvm/rubies/ruby-2.4.1/bin:/home/john/.rbenv/shims:/home/john/.rbenv/bin:/home/john/.rbenv/plugins/rubybuild/bin:/home/john/.rbenv/shims:/home/john/.rbenv/bin:/home/john/bin:/home/john/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/john/.rvm/bin

After some research, I found this. It says that this command

PATH="$(perl -e 'print join(":", grep { not $seen{$_}++ } split(/:/, $ENV{PATH}))')"

will safely remove duplicates. Is this true? Is there a better approach? Don't want to break anything.

I'm running Ubuntu 16.04

Upvotes: 0

Views: 252

Answers (2)

Walter A
Walter A

Reputation: 20022

OP asked for a script but liked my comment. Therefor I post this as an answer.

You can

echo "$PATH" | tr ':' '\n' | sort 

and see the doubles.

Do not try

# wrong command beneath
# PATH=$(echo "$PATH" | tr ':' '\n' | sort -u | tr '\n' ':')

because the order is important. Check and edit by hand.

Upvotes: 1

Gitarro
Gitarro

Reputation: 7

Why don't you just save the path string in a file and try if it works?

If it doesn't, you can just set it back by hand and everything will still be fine.

Upvotes: 0

Related Questions