JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Batch Check for Empty Variables

Author:JIYIK Last Updated:2025/03/22 Views:

This article explains how we can test if a variable has been set. We can use an if statement to check if a variable has been defined.


Batch check whether variables are empty

For a simpler context, we will start by defining two strings, str1 and str2. Our first string will be empty (undefined) and the second string will have a value (non-empty).

This is the script we will use.

@echo off
set str1=
set str2=Delft

::using the if statement, we will check whether str1 is an empty string.

if [%str1%]==[] echo "str1 is an empty string"
if [%str2%]==[] echo "str2 is an empty string"

pause
  1. We use set to get our input string where str1 has no value and str2 has the value Delft.
  2. We then use an if statement to test if our string is empty.

We usually use double colons ::to add comments in Batch scripts. The pause at the end will keep the CMD window until we press a key.

@echo offInstructs Command Prompt not to display the commands contained in a batch file, including itself.

Below is the output of our batch script.

Check if a variable is empty

As expected, our first string is empty.

In short, we can use the if statement to check if a string or variable is set, or in other words, defined. Remember to use square brackets instead of round brackets or double quotes.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Running Regular Expressions in Case Statements in Bash

Publish Date:2025/03/22 Views:75 Category:OPERATING SYSTEM

This article explores regular expressions, their basic syntax, and how to run them in Bash using case and if-else constructs. Introduction to Regular Expressions A regular expression, also called regex or regexp, is a sequence of characters

Floating-point arithmetic in Bash

Publish Date:2025/03/22 Views:189 Category:OPERATING SYSTEM

This short article describes how to quickly perform floating-point calculations in GNU BASH (shell scripts) directly at the command prompt or in a shell script. If you work with numbers, it can be helpful to perform quick floating-point cal

Writing to a File in Bash

Publish Date:2025/03/22 Views:133 Category:OPERATING SYSTEM

This article will let us explore different ways to write files in bash. Files can be appended or overwritten as required. Let's see how we can do it. Different ways to write/overwrite files in Bash We will see several operators, such as and

Terminating a Process in Bash

Publish Date:2025/03/22 Views:135 Category:OPERATING SYSTEM

This article will first discuss the different concepts related to Linux processes. After this, we will learn the different ways to terminate a process. Before going into the kill command, we must understand some preliminary concepts. Simple

Solve R command not found on Bash (or Cygwin)

Publish Date:2025/03/21 Views:102 Category:OPERATING SYSTEM

Commands may sometimes behave differently than you expect, even if it appears that you have done everything right, such as in the case of bash: '\r': command not found or similar error messages, for example syntax error near unexpected toke

How to fix the error Make Command Not Found in Cygwin

Publish Date:2025/03/21 Views:120 Category:OPERATING SYSTEM

Cygwin allows Windows users to access certain Linux features and includes a large number of GNU and open source tools that are commonly found in popular Linux distributions. When using Cygwin, it is easy to encounter a command not found err

Error handling in Bash

Publish Date:2025/03/21 Views:94 Category:OPERATING SYSTEM

This article introduces error handling in bash. Remember, understanding exit codes, options such as errexit and trap allow us to build robust scripts and manage bash problems more effectively. Exit Codes in Bash Handling errors based on exi

Getting the absolute path in Bash

Publish Date:2025/03/21 Views:60 Category:OPERATING SYSTEM

In this Bash article, we will learn different ways to get the absolute path in Linux. We will also learn some different Linux commands to get the absolute path of a file. Before we begin, we need to understand the basic concepts of absolute

Difference between Bash Nohup and &

Publish Date:2025/03/21 Views:151 Category:OPERATING SYSTEM

This short article introduces the nohup command and the control operator to run Linux processes in the background through Bash. In addition, we will further study the key differences between nohup and . Running Linux Processes in the Backgr

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial