From: Brian Candler Date: 2011-02-10T22:18:08+09:00 Subject: Re: System calls with ` in parameters Gerad S. wrote in post #980690: > Hi All, > > I am trying to add functionality to an interesting script. The part > that is tripping me up is that I have to make a system call to echo, and > feed several parameters. One of the parameters occasionally includes > the ` character. > > Because of this, the syntax of: > > `echo "#{someVar}" | ./some-script.py > "#{outputFile}"` > > is breaking when #{someVar} contains the tick `. Does this test case represent your problem? >> someVar="foo`bar" => "foo`bar" >> `echo "#{someVar}"` sh: Syntax error: EOF in backquote substitution => "" Quick workaround: use single quotes instead (but see below). >> `echo '#{someVar}'` => "foo`bar\n" The same occurs with system(): >> system("echo \"#{someVar}\"") sh: Syntax error: EOF in backquote substitution => false >> system("echo '#{someVar}'") foo`bar => true It's an issue with the shell, not with ruby; the same happens at the shell prompt too. $ echo "foo`bar" > bash: unexpected EOF while looking for matching ``' bash: syntax error: unexpected end of file The issue is that in the shell, double-quoted strings can *contain* backtick expansions, for example $ echo "foo`hostname`bar" foox100bar Backticks inside single quotes are not treated specially. However, single quotes inside single quotes are still a problem: >> someVar="foo'bar" => "foo'bar" >> `echo '#{someVar}'` sh: Syntax error: Unterminated quoted string => "" So using the Shellwords escaping mechanism, as already shown, is probably what you want. -- Posted via http://www.ruby-forum.com/.