Every little
knows how to source a ROS workspace, right? But what do they not tell you in the turorials? What they don’t want you to know??? How to unsource a workspace!
I’ve put together this bash script that does exactly that:
Example!
$ source ~/colonize_mars_ws/install/setup.bash
$ echo $LD_LIBRARY_PATH
# many entries
$ source unsource_ros.bash
$ unsource_ros
$ echo $LD_LIBRARY_PATH
# only your few initial entries!
$ source ~/colonize_venus_ws/devel/setup.bash
# now you can safely go on to colonizing Venus
# without the fear of dragging some Martians with you
It works both for ROS 1 and 2.
In ROS 1, it relies only on a list of known environment variables which it cleans. But if some packages install workspace setup scripts that also set other variables, there’s nothing that could be done (in ROS 1).
In ROS 2/colcon, thanks to the DSV files, the unsetting can work much more reliably. So, at least theoretically, in ROS 2, the unsource should be complete. However, crawling all DSV files can be slow, so this functionality has to be enabled via the --dsv option.
Somebody may ask why do I need this. Why I don’t just open up a new terminal. Yes, I could. However, one of the things the tutorials suggest (and I like it) is to put some sourcing into .bashrc. But hey, once you do that and you source, say, Kilted by default, how are you supposed to try Rolling? Or Jazzy? Hmm, edit your .bashrc? Ok, so ROS unsource is basically the answer. You can have a default ROS sourced, but you can also decide for something else and you should not carry all the burden from other distros with you!
The usual disclaimer: The script is provided as is. If you store your bitcoin wallet password in ROS_MY_WALLET_PATH, it will get smashed! There was some initial LLM-generated code I started from, but in the end I rewrote basically every part of it.