Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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:
***<><><||&&*&$PATH

You entered:
***<><><||&&*&$PATH

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
add comment

1 Answer

up vote 5 down vote accepted

Try doing this :

$ read -r -p 'Please enter a string >>> ' var
$ printf '%q\n' "$var"
\*\*\*\<\>\<\>\<\|\|\&\&\*\&\$PATH
share|improve this answer
1  
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 –  sputnick Feb 27 '13 at 16:28
add comment

Your Answer

 
discard

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.