Get raw input from command line (ignore shell meta characters)

10,695

Try doing this :

$ read -r -p 'Please enter a string >>> ' var
$ printf '%q\n' "$var"
\*\*\*\<\>\<\>\<\|\|\&\&\*\&\$PATH
Share:
10,695

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    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?

  • l0b0
    l0b0 about 11 years
    The -r is the magic option; see help read.
  • Gilles Quenot
    Gilles Quenot about 11 years
    No, the magic is %q