shellQuote function

String shellQuote(
  1. String arg
)

arg as a POSIX shell reads it back: plain when it holds only safe characters, else in double quotes with ", \, $ and ` escaped.

Implementation

String shellQuote(String arg) {
  if (arg.isNotEmpty && RegExp(r'^[A-Za-z0-9_./:@%+=,<>-]+$').hasMatch(arg)) {
    return arg;
  }
  return '"${arg.replaceAllMapped(RegExp(r'["\\$`]'), (m) => '\\${m[0]}')}"';
}