11 C
Jaipur
Tuesday, January 26, 2021

How to use bash if -z and if -n for testing strings in Linux

Must read

There are different string operators available in bash scripting language which can be used to test strings. The -z and -n operators are used to verify whether the string is Null or not. In this guide, we will test these string operators using the if statement in Centos 8.

In this guide you will learn about the following testing strings:

  • If-n when String is Null.
  • If-n when String is not Null
  • If-z when Sting is Null
  • If-z when String is not Null
  • If-z while taking String User input

Use of if -n operator when String is Null

Our main aim to use the if-n operator here is to show that the specific string is Null. Follow the cited steps to execute this task:

Create a new file with the name CheckString.sh. You can create it directly in your home directory or using the following command in the terminal:

$ touch CheckString.sh

You can see the CheckString.sh file has been created in your home directory as shown in the image.

Open the file CheckString.sh and write a bash script as it is as shown in the below image. Here you can see we have initialized a string with the name StdName followed by the value Null. In our if statement we have used the -n string test operator followed by the string StdName. As string StdName is null so that -n operator will execute the else section of the script.

Check for null value

Now we will run the below-mentioned command to check whether the string is Null or not.

$ bash CheckStrings.sh

As our string StdName is Null, so the -n string operator executed the else part. So the output will be Sorry! You have no name in the terminal as presented in the beneath image.

Run test script

Use of if -n when String is not Null

We are using the if-n operator here is to show that the specified string in the file is not Null. You have to go through the below-mentioned steps to perform this activity:

Do not need to create a new file or string. You will use the same file CheckString.sh for this task with a minor change. This time, the string StdName in your file must have some value other than null. So you have to assign your string a value (not null) while all the other script remains the same. So as you can see, we have assigned the string StdName a value Aqsa Yasin. So this time If part of the script will be executed by the -n operator.

if -n when String is not Null

Now we will run the below-mentioned command to check whether the string has some value or not.

$ bash CheckStrings.sh

As our string StdName has now a value Aqsa Yasin, so the -n string operator executed the if part. So the output will be Hy! Aqsa Yasin in the terminal as displayed in the appended image.

Test script result

Use of if -z when String is Null

We are using the if-z operator here is to show that the specified string in the file is Null. You have to go through the below-mentioned steps to perform this activity:

There is nothing new to do for this task. Just use the same old file CheckString.sh with the -z string operator. As we know -z operator executes the True part when the string is  Null. As string StdName has a null value so the if portion of the script will be performed by the -z operator.

if -z when String is Null

Now we will run the below-mentioned command to check that the string is null.

$ bash CheckStrings.sh

As our string StdName has now no value, and the -z string operator knows that the string is null, so it executes the if part. So the output will be Sorry You have no name in the terminal as presented in the affixed image.

Test result

Use of if -z when String is not Null

We are using the if-z operator here is to show that the specified string in the file is not Null. By walking through these steps you would be able to perform this activity:

No need to do anything new. We will be using the same old file CheckString.sh for this task with a minor change. This time, we will replace the -n string operator with the -z string operator. Also, you have to exchange if and else part of the script with each other. As we know -z operator executes the True part when the string is  Null. As string StdName has value Aqsa Yasin which is not null so the else statement will be executed by the -z operator.Advertisement

if -z when String is not Null

Now we will run the below-mentioned command to check whether the string is null or not.

$ bash CheckStrings.sh

As our string StdName has now a value Aqsa Yasin, and the -z string operator knows that the string is not null, so it executes the else part. So the output will be Hy Aqsa Yasin in the terminal as shown in the below image.

Test result

Use of if -z while Taking String User Input

Lastly, we are using the if-z operator to test the string input added by use at runtime. You have to perform the following steps to test the user string input:

This time you have to alter your script to some extent in the file. First of all, we are using the -z operator in the if statement followed by the variable string.  The system will ask the user to add some string values. The user will add some string. While using the read statement system will save the string written by the user in the variable string. As we know the -z operator executes the if part of the statement when the string is valid. If the string entered by the user is null, the system will ask you to enter the string again. If the string entered by the user is not null, the system will display the string.

if -z while Taking String User Input

Now we will run the below-mentioned command to check the string entered by the user:

$ bash CheckStrings.sh

When the system asked the user to add some string, the user doesn’t add any value. So -z operator recognizes the string as null so executed the if statement. The system prompted again to add some string value to the user. Now this time user added a valid string value New World Order. Hence the system displayed the message The string you entered is: New World

Script resultAdvertisement

Conclusion

In this guide, You’ve discovered how to use string operators to test a string value that is either null or non-null. I hope this article has assisted you a lot to get enough command on the string operators -z and -n and manipulate statements.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article