Rosanswers logo

Hey, so I need to compile my workspace every time I add it into my computer. This proves to be very time consuming. I would like to write a script that runs these commands:

roscd /directory/build
cmake ..

This is what I type into the terminal to compile all the directories and it works perfectly! But, when I tried just adding these commands to a script and running it I would get errors basically saying it didn't recognize the commands (i.e. roscd). I'm not sure if I'm supposed to source something so it will recognize the commands or something.

Any help is appreciated! George

Originally posted by GWall on ROS Answers with karma: 27 on 2014-07-09

Post score: 1

Original comments

Comment by allenh1 on 2014-07-09:
You have a good answer down there... But, I must ask... Why are you using cmake at all? With a catkin workspace, catkin builds (invoking CMake) all packages in the source directory in topological order. This is mainly for my own curiosity.

Comment by GWall on 2014-07-10:
Hey, thanks for your curiosity! When we started with ROS a catkin didn't exist. Then when it did we never made the transition, but we are planning on switching over pretty soon. Even with catkin I would like to have a script because its one less thing I need to setup when I install the system!


1 Answer 1


Rosanswers logo

You need to source your ros workspace's startup script in your own script's instance for all of your package/stack names to be recognized by roscd.

Just add the following snippet before your commands:

source ~/ros_ws/devel/setup.bash

Replace ros_ws with the path to your workspace.

Originally posted by tgd with karma: 76 on 2014-07-09

This answer was ACCEPTED on the original site

Post score: 4

Original comments

Comment by Martin Peris on 2014-07-09:
Not only that, but probably he also needs to source the ROS setup script before sourcing the workspace's startup script: source /opt/ros/<your_ros_distro>/setup.bash

Comment by GWall on 2014-07-10:
worked like a charm! Thanks!! @tgd

Comment by RDaneelOlivaw on 2018-04-17:
Mini bash script to test this:


echo "Ros Test START"
source /opt/ros/kinetic/setup.bash
source /home/user/catkin_ws/devel/setup.bash
roscd my_package
echo "Ros test END"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.