# untested sub bashquote { local $_ = shift; defined or die "Argument must not be undef"; /\x00/ and die "String must not contain NUL characters"; length or return "''"; # before adding "/" to safe characters: /^[A-Za-z0-9_]+$/ and return $_; m|^[A-Za-z0-9/_]+$| and return $_; s/'/'\''/g; s|((?:'\\''){2,})|q{'"} . (q{'} x (length($1) / 4)) . q{"'}|ge; $_="'$_'"; s/^''//; s/''$//; return $_; }