Shell Script To Check That Input Only Contains Alphanumeric Characters Last Updated : 09 Apr, 2021 Comments Improve Suggest changes Like Article Like Report If you want an input which contains only alphanumeric characters i.e. 1-9 or a-z lower case as well as upper case characters, We can make use of regular expression or Regex in short in a Shell Script to verify the input. Example: Input: Geeksforgeeks Output: True Explanation: Here all the inputted data are alphanumeric Input: Geeks@for@geeks Output: False Explanation: @ is not alphanumeric Here our task is to write a script to take inputs a variable, and it checks that the input string from start to end has only numbers or alphabets(lower or upper case). If there are any other special characters the condition in the while loop will evaluate to false and hence the while loop will get executed, and it inputs the variable again, and then it again checks for the while loop condition of alphanumeric characters. The loop will continue until the user inputs only alphanumeric and non-empty string or number. \ #!/bin/bash # Input from user read -p "Input : " inp # While loop for alphanumeric characters and a non-zero length input while [[ "$inp" =~ [^a-zA-Z0-9] || -z "$inp" ]] do echo "The input contains special characters." echo "Input only alphanumeric characters." # Input from user read -p "Input : " inp #loop until the user enters only alphanumeric characters. done echo "Successful Input" Output: Bash Script Output The following screenshot of the test case of the code is executed, and it only accepts non-empty alphanumeric input. It even rejects an empty input and other characters excluding alphabets and numbers. The following code is a regular expression to check for numbers or alphabets from start(^) till the end($) and a condition of empty input ( -z stands for the length of zero). So, the shell script will prompt the user again and again until he/she inputs an alphanumeric character. Comment More infoAdvertise with us Next Article Shell Script To Check That Input Only Contains Alphanumeric Characters M meetgor Follow Improve Article Tags : Linux-Unix Shell Script Similar Reads How to remove non-alphanumeric characters in PHP? Non-alphanumeric characters can be remove by using preg_replace() function. This function perform regular expression search and replace. The function preg_replace() searches for string specified by pattern and replaces pattern with replacement if found. Examples: Input : !@GeeksforGeeks2018? Output 2 min read How to Take Only a Single Character as an Input in Python Through this article, you will learn how to accept only one character as input from the user in Python. Prompting the user again and again for a single character To accept only a single character from the user input: Run a while loop to iterate until and unless the user inputs a single character.If 3 min read Bash Script to Check whether character entered is Upper, Lower, Digit, Consonant, or Vowel In Bash scripting, it's often necessary to analyze user input and determine the type of character entered. This information can be used for various purposes, like data validation, formatting, and logic control. This article demonstrates how to write a Bash script that checks whether a character ente 4 min read Shell Script To Check For a Valid Floating-Point Value User Input can be hectic to manage especially if it's about numbers. Let's say you wanted to perform mathematical calculations in your app or script. It's not hard to say that it will have to deal with floating-point numbers. It's easy in many statically typed programming languages but if it has to 4 min read Shell Script to Validate Integer Input Here we are going to see a shell script which validates an integer. We are going to display whether the input entered is an integer or an invalid input. Approach: Take input from the user.Store the input of any variableNow we are going to trim the input in such a way that all the -(minus) or +(plus) 1 min read Like