I’d like to run the following shell command from Ruby, which copies a string into the clipboard (on OS X), ‘n’ is suppressing the line break after the string caused by echo:
echo -n foobar | pbcopy
—> works, fine, now the clipboard contains “foobar”
I’ve tried the following, but all of them always copy the option ‘-n’ as well into the clipboard:
%x[echo -n 'foobar' | pbcopy]
%x[echo -n foobar | pbcopy]
system "echo -n 'foobar' | pbcopy"
system "echo -n foobar | pbcopy"
exec 'echo -n "foobar" | pbcopy'
`echo -n "foobar" | pbcopy`
IO.popen "echo -n 'foobar' | pbcopy"
What is the proper way to achieve this?
Your problem is that
-nis only understood by the bash built-inechocommand; when you say%x[...](or any of your other variations on it), the command is fed to/bin/shwhich will act like a POSIX shell even if it really is/bin/bash. The solution is to explicitly feed your shell commands to bash:You will, of course, need to be careful with your quoting on whatever
foobarreally is. The-cswitch essentially tells/bin/bashthat you’re giving it an inlined script: