Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there any way to read raw text from stdin before it is expanded or before the shell does anything to it? Say I wanted to run a script where a user enters a string which is then printed out, how can I get the following behavior:

$ sh test.sh
Please enter a string:

You entered:

Is there any way to implicitly surround the text with '' or escape all meta characters even if the user does not?

share|improve this question
up vote 6 down vote accepted

Try doing this :

$ read -r -p 'Please enter a string >>> ' var
$ printf '%q\n' "$var"
share|improve this answer
Thanks, +1 as soon as I get to 15 reputation! – gambol Feb 25 '13 at 2:25
The -r is the magic option; see help read. – l0b0 Feb 25 '13 at 16:30
No, the magic is %q – Gilles Quenot Feb 27 '13 at 16:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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