Bash User Input Validation

Posted on July 20th, 2008 in Tech Tips by gmendoza

If you are writing your latest and greatest Bash shell script that requires careful user input, then you are probably looking for a way to validate or sanitize the input before using the data in commands or subroutines. Here’s an example shell script that reads user input into a variable, which we in turn echo and sanitize into a new variable. The new variable will then be used to perform whatever function is required, in this case displaying the new value.

#!/bin/bash
read -p "Enter variable: " VAR_INPUT
# Sanitize input and assign to new variable
export VAR_CLEAN="`echo "${VAR_INPUT}" | tr -cd '[:alnum:] [:space:]'`"
echo "New Variable: ${VAR_CLEAN}"

Notice, we use the tr command to delete everything except alphanumeric and space characters. You can also perform further manipulation with any other command that comes to mind. For example, if you would like to also limit the number of characters to 10, use the cut command.

export VAR_CLEAN="`echo "${VAR_INPUT}" | tr -cd '[:alnum:] [:space:]' | cut -c -10`"

I like using tr in this fashion, because instead of trying to exclude specific characters, you have the option to enforce a deny all policy, making it easier for you to allow only what you want. Comments and ideas welcome!

For more information, be sure to check out the man pages for tr and take a look at the Advanced Bash-Scripting Guide.

7 Responses to 'Bash User Input Validation'

Subscribe to comments with RSS or TrackBack to 'Bash User Input Validation'.

  1. Amarendra said,

    on September 5th, 2008 at 3:53 am

    Interesting, and neat, especially the white-listing part.

  2. al said,

    on October 15th, 2008 at 12:49 pm

    I had something similar to complete not too long ago. I found it very hard to manipulate last characters going backwards in order to edit spaces. Example 10 character password al——–, where “-” is equal to space. In order to check for that and concatenate to just “al” instead of “al ” - what approach would you use?

  3. gmendoza said,

    on October 15th, 2008 at 12:55 pm

    Hi there. The example in the post explains that alphanumeric and spaces are allowed. Simply omit the [space] value, and you’ll be left strictly with alphanumeric. For example:

    tr -cd ‘[:alnum:]‘

  4. al said,

    on October 16th, 2008 at 7:18 am

    I actually did that. But please consider following - someone puts in the username “al_the_on___the_sea”. I wouldn’t want to shrink it to “altheon” after cutting 10 spaces, but would rather have “al_the_on” . This would make it 9 char, but if the last space was left “al_the_on_” it would be user difficult to use such a name. I tried different ways, but nothing easy came about. If you have some sort of solution, I would be all ears. Also from programming perspective, one would have to check from the back of the string moving forward until first alphanumeric character was found.

  5. gmendoza said,

    on October 16th, 2008 at 11:17 am

    Easy… use sed to strip beginning and trailing spaces:

    tr -cd ‘[:alnum:] [:space:]‘ | sed -e ’s/^[ ]*//’ -e ’s/[ ]*$//’

  6. al said,

    on October 16th, 2008 at 3:08 pm

    Thanks a bunch - I’ll remember this one.

  7. buddyh said,

    on October 21st, 2008 at 4:27 am

    As a learning admin this is great info. I thought there was a way to limit the input to a specific set of characters. I just need to have the user input a Y or N in either upper or lower case and reject any other entry. Thinking of using a while loop till a correct char is entered as an alternative.
    Tx in advance

Post a comment