In a bash script, how do I sanitize user input?

前端 未结 6 1092
抹茶落季
抹茶落季 2020-12-24 05:01

I\'m looking for the best way to take a simple input:

echo -n \"Enter a string here: \"
read -e STRING

and clean it up by removing non-alph

6条回答
  •  无人及你
    2020-12-24 06:05

    After a bit of looking around it seems tr is indeed the simplest way:

    export CLEANSTRING="`echo -n "${STRING}" | tr -cd '[:alnum:] [:space:]' | tr '[:space:]' '-'  | tr '[:upper:]' '[:lower:]'`"
    

    Occam's razor, I suppose.

提交回复
热议问题