Sourcing Files in Bash Profile using Wildcard

I have been trying out ROS on my Ubuntu 20.04 machine and I just crated my first workspace by going through the documentation.

Later on I needed to source the setup files inside this directory so that it can overlay on the already set bash setup files for ROS. My question is, since I will be having several such projects in the same location, can I use wildcard to source the setup.bash files from my work folders?

For example., on my machine I will have all ROS projects in a common standard folder which is:

/home/myname/Projects/ros-projects/workspace-1/

Likewise If I have another project, it will be:

/home/myname/Projects/ros-projects/workspace-2/

Now the setup.bash is to be found under each workspace in a folder called devel. So the question is, can I do the following in my .bash_profile?

source /home/myname/Projects/*/*/devel/setup.bash

Is that is not directly possible, can I write a shell script to do that? Any ideas?

1 Answer

source is an internal bash command that does only accept a single filename as argument. When you supply wildcards, only the first matching file will be processed.

To process multiple files, you can use the find command. source, however, is a shell builtin, i.e., not a command corresponding with an executable in your search path. Using the -exec option or xargs therefore will not work because these constructs only recognize executable files. Following construct, however, can work (with thanks to steeldriver).

while read -r f ; do source "$f" ; done < <(find /home/myname/Projects -wholename '*/devel/setup.bash')
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like