The -z actually checks if length of the variable is zero or not. The single bracket is the old Posix convention and it has some shortcomings.
I hope this quick little tutorial helped you in comparing strings in bash shell scripts. I recommend reading another quick tutorial on bash sleep command as well. Please enter at least 3 characters 0 results found. Abhishek Prakash. In this tutorial you'll learn how to compare strings in bash shell scripts. You'll also learn to check if a string is empty or null. Table of Contents. Quick Links. Search Forums.
Show Threads. Show Posts. Shell Programming and Scripting. How to check for null or empty string. Registered User. Join Date: Jul Please modify this script Code :. Last edited by Yogesh Sawant; at AM.. Reason: added code tags. Join Date: Dec Please read our rules and refrain from bumping your posts. Thanks, ZB. Originally Posted by zazzybob.
You shuld post the solution to help other forum members having same type of problems in coming future Cheers, Awadhesh. Originally Posted by doer. Code :. Parsing null or empty output. I am working an some if then statements for a script.
There is a difference between a variable being empty and a variable being unset. From the question's title, this appears to be the distinction you are trying to make, but it is unclear from the question if indeed you care or are even aware of this distinction. Do you care about that distinction? If so test -z will not help. Add a comment.
Active Oldest Votes. Improve this answer. It can be more clear for the one reading the code that you want to see if the var is empty. I would leave the string comparison for the cases in which you are comparing with a non-empty string. Chock if a variable is set!
0コメント