public static String escapeShell ( final String s ) { if ( null == s ) { return s ; } if ( s . startsWith ( "'" ) && s . endsWith ( "'" ) ) { return s ; } else if ( s . startsWith ( "\"" ) && s . endsWith ( "\"" ) ) { return s . replaceAll ( "([\\\\`])" , "\\\\$1" ) ; } return s . replaceAll ( "([&><|;\\\\`])" , "\\\\$1" ) ; }
Escape characters meaningful to bash shell unless the string is already surrounded in single quotes