% bash_esc - Convert string into escaped version for use in command lines
% bash_esc :: string -> string
function out=bash_esc(in,method)
	import java.io.*;
	if nargin<2, method=1; end

	cs=feature('DefaultCharacterSet');
	runtime=java.lang.Runtime.getRuntime();

	switch method
	case 1 % ALternative 1
		process=runtime.exec({'bash', '-c', 'printf "%q" "$(cat)"'});
		writer=OutputStreamWriter(process.getOutputStream(),cs);
		writer.write(in); writer.close();

	case 2 % ALternative 2
		process=runtime.exec('bash');
		writer=BufferedWriter(OutputStreamWriter(process.getOutputStream(),cs));
		writer.write('printf "%q" "$(cat)"'), writer.newLine();
		writer.write(in); writer.close();
	end

	rdr=BufferedReader(InputStreamReader(process.getInputStream(),cs));
	out=char(rdr.readLine());
	process.destroy();
end
