Inside the shell to determine if the string is empty

Source: Internet
Author: User

There are several main ways to do this:

echo "$str" |awk ' {print length ($)} '
Expr Length "$str"
echo "$str" |wc-c
But the third one will be 1 more, maybe the terminator is counted.

There are three ways to determine that a string is empty:
If ["$str" = ""]
If [x "$str" = x]
If [-Z "$STR"] (-n is not null)
Note: You must substitute double quotes, otherwise some commands will complain, develop good habits.

I just always forget to add double quotes, I am really a beginner ...


Original Address http://im00100.blog.163.com/blog/static/76046672008091113817/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.