- Without the full launch file from the snippet you posted, it's difficult to say exactly why they are using the
<env>
tag. Presumably, they are starting a node (gazebo_ros?) that uses the value of the GAZEBO_MODEL_PATH
environment variable. They are using the <env>
tag to modify the environment variables that one or more nodes are executed with.
- Environment variables (e.g. PATH, PYTHONPATH, ROS_PACKAGE_PATH, etc.) generally use colons to separate values if there are multiple values. This is a Unix concept not a ROS concept. The
$(env)
and $(optenv)
and $(arg)
substitution args can expand values using environment variables from the environment that roslaunch
was invoked from. If you use multiple words separated with spaces for the default_value
in the optenv substitution arg, then that is exactly what the value will expand to (assuming the ENVIRONMENT_VARIABLE value is not set).
- You don't ever need to use either of these, but if you'd like environment variables that have been set in your terminal to expand into values in a launch file, then you could use the substitution args. As an example, let's assume you've written a node that takes in an argument, and that you are going to pass a value for this argument to the node using the
args
attribute of the <node>
tag. If you used the optenv
expansion arg to set the value of the argument, then, you'd be able to decide the value of the argument simply by setting an environment variable.
As an example consider the following launch file that runs a single Python script:
Launch file
<launch>
<env name="TESTVAR" value="$(optenv INTVAR /home/user/default_directory1:/home/user/default_directory2)" />
<node name="env_test" type="environment_reader.py" pkg="test_package" output="screen"/>
</launch>
Python script
#!/usr/bin/env python
import os
print "\r\n"
print "TESTVAR = ", os.environ['TESTVAR']
print "\r\n"
Now consider the following bash commands to illustrate the functionality of the environment variables and the substitution args:
user@hostname:~/Desktop⟫ roslaunch test_package tmp.launch
... logging to /home/user/.ros/log/b401c412-30fa-11e7-a2a4-28d2448c624c/roslaunch-hostname-11437.log
Checking log directory for disk usage. This may take awhile.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.
started roslaunch server http://hostname.local:35062/
SUMMARY
========
PARAMETERS
* /rosdistro: indigo
* /rosversion: 1.11.21
NODES
/
env_test (test_package/environment_reader.py)
auto-starting new master
process[master]: started with pid [11449]
ROS_MASTER_URI=http://localhost:11311/
setting /run_id to b401c412-30fa-11e7-a2a4-28d2448c624c
process[rosout-1]: started with pid [11462]
started core service [/rosout]
process[env_test-2]: started with pid [11465]
TESTVAR = /home/user/default_directory1:/home/user/default_directory2
[env_test-2] process has finished cleanly
log file: /home/user/.ros/log/b401c412-30fa-11e7-a2a4-28d2448c624c/env_test-2*.log
^C[rosout-1] killing on exit
[master] killing on exit
shutting down processing monitor...
... shutting down processing monitor complete
done
user@hostname:~/Desktop⟫ export INTVAR=/different/directory
user@hostname:~/Desktop⟫ roslaunch test_package tmp.launch
... logging to /home/user/.ros/log/c58bed7a-30fa-11e7-9984-28d2448c624c/roslaunch-hostname-11933.log
Checking log directory for disk usage. This may take awhile.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.
started roslaunch server http://hostname.local:43511/
SUMMARY
========
PARAMETERS
* /rosdistro: indigo
* /rosversion: 1.11.21
NODES
/
env_test (test_package/environment_reader.py)
auto-starting new master
process[master]: started with pid [11952]
ROS_MASTER_URI=http://localhost:11311/
setting /run_id to c58bed7a-30fa-11e7-9984-28d2448c624c
process[rosout-1]: started with pid [11966]
started core service [/rosout]
process[env_test-2]: started with pid [11969]
TESTVAR = /different/directory
[env_test-2] process has finished cleanly
log file: /home/user/.ros/log/c58bed7a-30fa-11e7-9984-28d2448c624c/env_test-2*.log
^C[rosout-1] killing on exit
[master] killing on exit
shutting down processing monitor...
... shutting down processing monitor complete
done
Originally posted by jarvisschultz with karma: 9031 on 2017-05-04
This answer was ACCEPTED on the original site
Post score: 4
Original comments
Comment by shawnysh on 2017-05-05:
Thanks for your detailed explanation! You are really sweet and thoughtful! Thanks a lot! You are a great teacher!
Comment by shawnysh on 2017-05-05:
one more question, in official doc
$(env ENVIRONMENT_VARIABLE)
Substitute the value of a variable from the current environment. The launch will fail if environment variable is not set. This value cannot be overridden by <env> tags.
Why it says 'This value cannot be overridden by env tags'?
Comment by jarvisschultz on 2017-05-05:
This means that the $(env)
substitution arg will only read from the environment that the launch file was launched from. If you were to set an environment variable with the <env>
tag, the $(env)
substitution arg would not pick up on that variable.
Comment by anikk on 2023-08-06:
you deserve a prize for this answer