I'm writing a bit of Ruby for use on the command line, but the shell is interfering too much - is there a way to turn off interpolation for the entire line? I know I can escape certain characters, but I'm looking for a blanket ban.

For example, I've got the shell set not to add to history if it begins with a blank space.

Any help with this is much appreciated.

I don't think I made myself clear, my apologies. An example:

ruby -pi -e 'gsub(/(find)(this)/, "#{$1} #{$2}")'

The shell won't interpolate those $ numbers because it's wrapped in single quotes, but sometimes the code will need to include quotes. Ruby is quite helpful in this, you can use other special characters to change quotes but then it all starts getting a bit messy, and it won't be as straightforward as above. What I'd like is something more like:

%% ruby -pi -e 'gsub(/(find)(this)/, "#{$1} #{$2}")'

Where %% would turn of interpolation by the shell for the rest of the line.

  • Can you give us an example ? – Iain Aug 15 '12 at 11:41
  • What do you mean by interpolation? My understanding of it doesn't make any sense in your context. – KingyCronus Aug 15 '12 at 12:49
  • @KingyCronus I want the shell to ignore any special characters, things it thinks are variables etc etc i.e. the line I give is given to the ruby interpreter as is, literal. I don't want to have to escape anything. – Iain Aug 15 '12 at 14:05

How about using a function that will read from a prompt and then execute literally what you type in?

function f { read -p"> " c; $c; }

In use, it will look like:

$ f
> echo $var
  • I'm going to give this a try, it's a very interesting idea! +1 for a bit of sideways thinking :) – Iain Aug 17 '12 at 19:32

When you create your command line, continue to use the single quotes, but escape (use ) any internal single-quotes and will replace them with single-quotes without ending the quoting of the string as a whole.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.