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

How to build lnmp environment in docker

Create a project directory mkdir php Create the f...

DIV background semi-transparent text non-translucent style

DIV background is semi-transparent, but the words ...

An article to help you understand the basics of VUE

Table of contents What is VUE Core plugins in Vue...

MySQL optimization solution: enable slow query log

Table of contents Preface Setting up slow query l...

Vue monitoring properties and calculated properties

Table of contents 1. watch monitoring properties ...

Vue implements simple comment function

This article shares the specific code of Vue to i...

Detailed explanation of CSS multiple three-column adaptive layout implementation

Preface In order to follow the conventional WEB l...

The most complete package.json analysis

Table of contents 1. Overview 2. Name field 3. Ve...

Detailed explanation of mixins in Vue.js

Mixins provide distributed reusable functionality...

Detailed process of SpringBoot integrating Docker

Table of contents 1. Demo Project 1.1 Interface P...

CentOS6.8 Chinese/English environment switching tutorial diagram

1. Introduction People who are not used to Englis...