a string containinng character
if echo "${word}" | grep '[0-9]' >/dev/null; then echo 'Invalid input' fi string='My string'; if [[ $string =~ "My" ]]; then echo "It's there!" fi string='My long string' if [[ $string == *"My long"* ]]; then echo "It's there!" fi [[ ! $VAR =~ [0-9] ]]; # contains only alphabets and numbers $VAR =~ ^[[:alnum:]]+$ # Or $VAR =~ ^[0-9a-zA-Z]+$ #!/bin/bash while true; do read -r -p "Enter a string: " VAR if [[ $VAR =~ ^[[:alnum:]]+$ ]];then echo "OK: Contains alphabets and numbers" else echo "NOK: Contains special character" fi done #!/bin/bash while true; do read -r -p "Enter a string: " VAR if [[ $VAR =~ ^[0-9]+$ ]];then echo "Input contains number" else echo "Input contains non numerical value" fi done
reference:
date command in bash
#!/bin/bash today = `date +%m-%d-%Y` echo $today
Replace String in a File with the `sed` Command
sed -i 's/search_string/replace_string/' filename
Control and comparison
大于 -gt (greater than) 小于 -lt (less than) 大于或等于 -ge (greater than or equal) 小于或等于 -le (less than or equal) 不相等 -ne (not equal) if (( $(echo "$num1 > $num2" |bc -l) )); then … fi ############### and if [ c1 -a c2 ]; then … fi if [ c1 ] && [ c2 ]; then … fi ############## or if [ c1 -o c2 ]; then … fi if [ c1 ] || [ c2 ]; then … fi
multiline comments in Bash
: ' This is a very neat comment in bash '