Summary of shell's method for determining whether a variable is empty

Summary of shell's method for determining whether a variable is empty

How to determine whether a variable is empty in shell

In shell programming, the error checking items for parameters include whether the variable is assigned a value (that is, whether a variable is empty). The method to determine whether a variable is empty is as follows:

1. Variables are enclosed in " " quotes

#!/bin/sh
para1=
if [ ! -n "$para1" ]; then
  echo "IS NULL"
else
  echo "NOT NULL"
fi

[Output result] "IS NULL"

2. Judge directly by variables

#!/bin/sh
para1=
if [ ! $para1 ]; then
  echo "IS NULL"
else
  echo "NOT NULL"
fi

[Output result] "IS NULL"

3. Use test to judge

#!/bin/sh
dmin=
if test -z "$dmin"
then
  echo "dmin is not set!"
else
    echo "dmin is set !"
fi

【Output result】"dmin is not set!"

4. Use "" to judge

#!/bin/sh
 dmin=
if [ "$dmin" = "" ]
then
  echo "dmin is not set!"
else
    echo "dmin is set !"
fi

【Output result】"dmin is not set!"

You may also be interested in:
  • Detailed explanation of advanced usage examples of shell variables
  • Handling variables with spaces in shell script (bash script)
  • Numerical calculation of shell variables in Linux
  • Detailed explanation of special variables and extended variables in Shell programming
  • Shell programming variable numerical calculation method example
  • How to pass the value of the shell for loop variable to other shell scripts
  • Detailed explanation of variable numerical calculation in Shell programming (Part 2)
  • Detailed explanation of variable numerical calculation in Shell programming (I)
  • A brief explanation of the meaning of shell variables $#, $@, $0, $1, $2 in Linux
  • Determine whether the Linux Shell environment variable exists
  • Detailed explanation of variable types in Linux bash Shell
  • Linux Shell Script Series Tutorial (Part 4): Using Functions to Add Environment Variables
  • Linux Shell Script Tutorial Series (Part 3): Variables and Environment Variables
  • Detailed explanation of shell variables

<<:  How to use regular expression query in MySql

>>:  A brief talk about the knowledge you need to master when getting started with Vue

Recommend

Detailed explanation of how to use element-plus in Vue3

Table of contents 1. Installation 2. Import in ma...

Vue implements card flip carousel display

Vue card flip carousel display, while switching d...

How to solve the element movement caused by hover-generated border

Preface Sometimes when hover pseudo-class adds a ...

8 JS reduce usage examples and reduce operation methods

reduce method is an array iteration method. Unlik...

Detailed explanation of Vue two-way binding

Table of contents 1. Two-way binding 2. Will the ...

The difference between MySQL user management and PostgreSQL user management

1. MySQL User Management [Example 1.1] Log in to ...

A time-consuming troubleshooting process record of a docker error

Table of contents origin Environmental Informatio...

How to set underline in HTML? How to underline text in HTML

Underlining in HTML used to be a matter of enclos...

Share 13 basic syntax of Typescript

Table of contents 1. What is Ts 2. Basic Grammar ...

Detailed explanation and examples of database account password encryption

Detailed explanation and examples of database acc...

Differences between MySQL CHAR and VARCHAR when storing and reading

Introduction Do you really know the difference be...

Install Docker on Linux (very simple installation method)

I have been quite free recently. I have been doin...

Discussion on more reasonable creation rules for MySQL string indexes

Preface Regarding the use of MySQL indexes, we ha...