Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying $PATH variable by a bash alias

Tags:

alias

bash

path

I created this alias in my .bashrc file:

alias changepath="export PATH=$JAVA_HOME/bin:$PATH"

What I want to do is change $JAVA_HOME and then change the $PATH to include the new $JAVA_HOME/bin.

When I change $JAVA_HOME and type this command instead of using the alias, it works as expected. However, when I change $JAVA_HOME and then use the alias, the changepath command prepends the old $JAVA_HOME/bin instead of the new $JAVA_HOME/bin to $PATH.

How can I fix this?

like image 442
salmonalive Avatar asked Apr 07 '26 11:04

salmonalive


1 Answers

You can also defer evaluation of variables by single-quoting your alias definition, i.e.

alias changepath='export PATH=$JAVA_HOME/bin:$PATH'

Note that the alias or function solutions do not solve the problem of clearing any previous JAVA_HOME you have set.

So you can wind up with a crazy PATH

 PATH="/usr/bin:/sys"

 PATH=/usr/bin/java:/usr/bin:/sys

 PATH=/usr/bin/java/bin:/usr/bin/java:/usr/bin:/sys
like image 75
shellter Avatar answered Apr 10 '26 16:04

shellter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!