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

As dj_segfault points out, the shell can do most of this for you. Looks like you'll have to fall back on something external for lower-casing the string, though. For this you have many options, like the perl one-liners above, etc., but I think tr is probably the simplest.

As dj_segfault points out, the shell can do most of this for you. Looks like you'll have to fall back on something external for lower-casing the string, though. For this you have many options, like the perl one-liners above, etc., but I think tr is probably the simplest.

# first, strip underscores CLEAN=${STRING//_/} # next, replace spaces with underscores CLEAN=${CLEAN// /_} # now, clean out anything that's not alphanumeric or an underscore CLEAN=${CLEAN//^a-zA-Z0-9_/} # finally, lowercase with TR CLEAN=`echo -n $CLEAN | tr A-Z a-z` The order here is somewhat important. We want to get rid of underscores, plus replace spaces with underscores, so we have to be sure to strip underscores first.By waiting to pass things to tr until the end, we know we have only alphanumeric and underscores, and we can be sure we have no spaces, so we don't have to worry about special characters being interpreted by the shell.

Bash can do this all on it's own, thank you very much. If you look at the section of the man page on Parameter Expansion, you'll see that that bash has built-in substitutions, substring, trim, rtrim, etc. To eliminate all non-alphanumeric characters, do CLEANSTRING=${STRING//^a-zA-Z0-9/} That's Occam's razor.No need to launch another process.

Well put, great answer. I was using parameter expansion without even realizing it. – Devin Reams Sep 18 '08 at 17:18 It is a good answer for a subset of the specifications, but it doesn't change spaces to underscores.

– Axeman Sep 18 '08 at 18:55.

Quick and dirty: STRING=\echo 'dit /ZOU/ een test123' | perl -pe's/ /_/g;tr/A-Z/a-z/;s/^a-zA-Z0-9_//g.

I hadn't seen your solution when I went to work on mine. – Axeman Sep 18 '08 at 3:43.

You could run it through perl. Export CLEANSTRING=$(perl -e 'print join( q//, map { s/\\s+/_/g; lc } split /^\\s\\w+/, \$ENV{STRING} )') I'm using ksh-style subshell here, I'm not totally sure that it works in bash. That's the nice thing about shell, is that you can use perl, awk, sed, grep....

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions