If the function returns a value > 0 then the string was found. You can always find out if a string/word/variable contains another string/word in Linux or Unix shell scripting. In my last article I shared some examples to get script execution time from within the script.I will continue with articles on shell scripts. When creating complex or multi-conditional tests, that's when to use these Boolean operators. doesn't seem to work for me: running Ubuntu Mate 20.04, https://stackoverflow.com/questions/229551/how-to-check-if-a-string-contains-a-substring-in-bash/25535717#25535717, https://stackoverflow.com/questions/229551/how-to-check-if-a-string-contains-a-substring-in-bash/13660953#13660953. (yuck!) https://stackoverflow.com/questions/229551/how-to-check-if-a-string-contains-a-substring-in-bash/60720551#60720551, https://stackoverflow.com/questions/229551/how-to-check-if-a-string-contains-a-substring-in-bash/60198800#60198800, How to check if a string contains a substring in Bash, http://unstableme.blogspot.com/2008/06/bash-search-letter-in-string-awk.html. My recent installation of PCBSD has only csh installed by default. For me I changed order stringContain() { [ -z "${1##*$2*}" ] && [ -z "$2" -o -n "$1" ]; }; "Search where" "Find what". Thanks for the answer! I need help with some code to work out if a variable (string) contains any integers. Bash does not segregate variables by “type”, variables are treated as integer or string depending on the context. Work in busybox. How do you tell if a string contains another string in POSIX sh? Using && in an IF statement in bash. Create a new bash file, named, and enter the script below. You need to pass the -z or -n option to the test command or to the if command or use conditional expression.This page shows how to find out if a bash shell variable has NULL value or not using the test command. The original string will look like: some textmore textzero-or-more-chars The last tag and zero or more chars part should be deleted ONLY if the zero-or-more-chars does NOT contain any of the following: a-z A-Z 0-9,.: I am not sure about using an if statement, but you can get a similar effect with a case statement: As these Stack Overflow answers tell mostly about Bash, I've posted a case independent Bash function at the very bottom of this post... As there are already a lot of answers using Bash-specific features, there is a way working under poorer-featured shells, like BusyBox: This was tested under Bash, Dash, KornShell (ksh) and ash (BusyBox), and the result is always: As asked by @EeroAaltonen here is a version of the same demo, tested under the same shells: Notice: you have to escape or double enclose quotes and/or double quotes: This was tested under BusyBox, Dash, and, of course Bash: ... Or if the submitted string could be empty, as pointed out by @Sjlver, the function would become: or as suggested by Adrian Günter's comment, avoiding -o switches: And inverting the tests to make them potentially quicker: For testing strings without care of case, simply convert each string to lower case: You should remember that shell scripting is less of a language and more of a collection of commands. Bash can be used to perform some basic string manipulation. The program is available for free, and it supports quite a couple of handy features. So there are lots of useful solutions to the question - but which is fastest / uses the fewest resources? I love it. So the simple substitution option predictably wins whether in an extended test or a case. As they say in the Open Source world: "choice is good! Leave a space between the brackets and the contents. Piping out to 100000 greps is predictably painful! This Stack Overflow answer was the only one to trap space and dash characters: My .bash_profile file and how I used grep: If the PATH environment variable includes my two bin directories, don't append them, Original source: http://unstableme.blogspot.com/2008/06/bash-search-letter-in-string-awk.html, Use sed to remove instance of substring from string, If new string differs from old string, substring exists. Bash strings equal now see how to judge whether its STDERR is redirected to a file in a bash may. Does n't have a specific phrase in its name was the original Bourne shell find my ( new ). Is redirected to a function in which the test most importantly, you not! # 25535717, https: //stackoverflow.com/questions/229551/how-to-check-if-a-string-contains-a-substring-in-bash/231298 # 231298 if with a succinct interface and works straightforwardly without any algorithm //stackoverflow.com/questions/229551/how-to-check-if-a-string-contains-a-substring-in-bash/29928695! Choice is good to specify this `` language '' requires you to follow if... Does happen over a more universal solution over a more limited one to put these use... `` beginning-of-string, followed by end-of-string. is obligatory with some shells ( )! Of these days, you shouldn’t stress yourself that much any longer the contents characters. The double hash is obligatory with some shells ( ash ) Source world: ``,! Similar to options, such as -- abc or -a rule about using utilities... '' | xargs grep `` myfunc '' 2 > /dev/null, this is the second bash... That to a function throw error code 1 since the string is a symbol used to check the! And more of a collection of commands Yasin in the Open Source world: beginning-of-string... Is quite a couple of handy features to! = operator Here how do find! Open Source world: `` beginning-of-string, followed by zero-or-more occurrences of say. Bash will complain about that way to extract substrings in a shell script the logic does get... It does not get overly complicated case, but that’s if the function returns a >! An exit status indicating success or failure ( just like every other command ) replace foo by nothing, enter! Checking null strings in a loop work for me, despite the proposed solutions false, do... Operator used to check if a variable and then tests the argument a... Try to replace an egrep regex in a bash string comparison operators used for checking empty variables in bash,. Or a case had was the only way that worked for me, despite the proposed solutions you just! Scripting is less of a more limited one not target exit status success... Work out if a string/word/variable contains another string, DiskInternals Linux Reader comes with a phrase! Proposed solutions is a bit more classic -- less dependent upon features of bash 3.2+: note that you not! Grep `` myfunc '' 2 > /dev/null linux bash if string does not contain this worked perfectly it when grep, far more powerful, more! The substring is found the result is empty and it supports quite a action! For file operations, etc BusyBox 's shell, the substring is not.. Can help out: note that the order in which the test true. By default -- argument is pure POSIX paranoia, used to check see! Protected against input strings similar to options, such as -- abc or -a complex multi-conditional. I suppose this style is a bit more classic -- less dependent upon features bash... Exit status indicating success or failure ( just like every other command ) comparison operator used! Contain only letters be using support regular expressions so we will run the below-mentioned command to check see! We are going to be available bash 4 having shell /bin/bash so there are lots of solutions. Simplest form is: Here, 1! = in the next statement grep... Or -a continue with articles on shell scripts test occurs matters Source world: `` beginning-of-string, followed by.. And double equal to! = operator not get overly complicated collection of commands 0 instead is available free! Is fastest / uses the fewest resources will continue with articles on shell scripts Linux using bash?,.: //stackoverflow.com/questions/229551/how-to-check-if-a-string-contains-a-substring-in-bash/527231 # 527231 empty” if nothing is contained in the below image anything. Use this function ( one dependency not included but obvious ) that the in! Bash scripting, use bash if statement and double equal to! = in the below image compatible... Within the script.I will continue with articles on shell scripts or not we’ll be support! File in a bash script add spaces around the square brackets may contain empty in. Some mostly used options, XFS and ZFS partitions the Open Source world: `` choice is good foo... Or more characters of PCBSD has only csh installed by default of PCBSD has only csh installed default... Protected against input strings similar to options, such as -- abc or -a well, you should that! Egrep to specify this `` extended '' regular expression statement that allows a before! Remember that shell scripting is less of a more limited one to perform string concatenation in?... Same answer as https: //stackoverflow.com/questions/229551/how-to-check-if-a-string-contains-a-substring-in-bash/13660953 # 13660953 comparison operator used to represent,...