Conditions in BASH scripts Pt. 1

This is just a reminder for myself. Usually I have to look this up in Google whenever I want to use it :-(
Check if string A contains string B:

if [[ $A == *"$B"* ]] then echo We have a hit else echo No hit fi

Check if a variable A is a number:

if [ "$A"=~"^[0-9]+$" ] then echo Variable contains only digits else echo This is not a number fi

About Juergen Caris

I am 54yo, MSc(Dist) and BSc in Computer Science, German and working as a Senior Server Engineer for the NHS Lothian. I am responsible for the patient management system, called TrakCare. I am a UNIX/Linux guy, working in this sector for more than 20 years now. I am also interested in robotics, microprocessors, system monitoring, Home automation and programming.
This entry was posted in Bash, Beginner. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>