Unlike some other languages like C++, in Bash you can check whether the string is null or empty with one single command: if [ -z "$VAR" ] The -z actually checks if length of the variable is zero or not. If the variable is not set or if it is empty (equal to “”), the leng...
Bash string comparison It is advisable always to check and compare if two strings are equal in a Bash script; this is quite important for different reasons. If two strings are equal in a Bash script, it implies that both strings have the same length and character sequence. The “if” stat...
Check if the string is not empty The-nflag checks if the string length is non-zero. It returns true if the string is non-empty, else it returns false if the string is empty: $ [ -n "sam" ] && echo "True" || echo "False" Check if the string is empty The-zflag checks whethe...
-eq=> Integer X is equal to Integer Y-ne=> Integer X is not equal to Integer Y-gt=> Integer X is greater than Integer Y-ge=> Integer X is greater than or equal to Integer Y-lt=> Integer X is lesser than Integer Y-le=> Integer X is lesser than or equal to Integer Y Every ...
if [ "$string1" = "$string2" ]; then echo "The strings are equal." else echo "The strings are not equal." fi File Existence Check with if-else #!/bin/bash file_path=/var/scripts/migratedb.sh if [ -e "$file_path" ]; then ...
if [[ -n $VAR ]]; then echo "The string is not empty." else echo "The string is empty." fi 2. Run the Bash script and check the result: The script tests the$VARvariable and outputs that the string is not empty. Comparing Strings with Case Statements ...
file f1 is newer than f2. f1 -ot f2 file f1 is older than f2 Comparison Operators Comparison operators are used in bash to compare two strings to check if they are equal or not. Here, we will list some comparison operators including, string, and integer operators. Integer Operators Operat...
"which is not read as a comment. First line is also a place where you put your interpreter which is in this case: /bin/bash. Here is our first bash shell script example: #!/bin/bash # declare STRING variable STRING="Hello World" #print variable on a screen echo $STRING...
In the above example, the code inside the "if" block will be executed because the condition! [ $num -eq 10 ]evaluates to true. The string "The number is not equal to 10." will be printed. Conclusion In this guide, you learned how to negate an "if" condition in Bash using the!(...
not equal:!= numeric equality:-eq not equals:-ne is empty:-z if[[1-eq1]];if[[-z$USER]]; Elif if[[-z$USER]];thenecho"user is empty"elif[[1-eq1]];thenecho"1==1"elseecho"false"fi Ternary [[$USER='username']]&&echo"yes"||echo"no" ...