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
Related videos on Youtube
Author by
Admin
Updated on September 18, 2022Comments
-
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 about 11 yearsThe
-r
is the magic option; seehelp read
. -
Gilles Quenot about 11 yearsNo, the magic is
%q