Only replace slashes if the argument is defined

Fixes #2920