Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5957827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:27:24+00:00 2026-05-22T18:27:24+00:00

I have written a function which checks whether one or more variables passed as

  • 0

I have written a function which checks whether one or more variables passed as arguments are empty (got the idea from here)

Here is what the script looks like:

#!/bin/bash
is_variable_defined?() {
 for VAR in "$@"; do
  : "${!VAR? "ERROR: $VAR is undefined."}"
 done
}

TEST='a'

is_variable_defined? TEST TEST2

And here is the output:

/path/to/script.sh: line 4: !VAR:  ERROR: TEST2 is undefined.

However what I would like to output is:

TEST2 is undefined

I have tried tweaking the : "${!VAR? "ERROR: $VAR is undefined."}" line but whatever I do it breaks.

Does anybody know what to modify in the script to get the output I want?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:27:25+00:00Added an answer on May 22, 2026 at 6:27 pm

    Are you seeing if the variable is undefined or simply a null string?

    Null Test

    is_variable_defined() {
        for var in $@
        do
            if [ -z ${!var} ]   # Or if [[ ${!var} ]]
            then
                echo "Var $var has no value"
            fi
        done
    }
    

    Undefined Test

    is_variable_defined() {
    for var in $@
    do
         if ! set | grep -q "^$var="
         then
            echo "Var $var is not set"
         fi
    done
    

    The last is looking at the output of the set command to see if the variable is listed there. The -q will make grep quiet. And you look at the exit status to see if it found the string. If your grep doesn’t support the -q parameter, you can try grep "^$var=" 2> /dev/null.


    Using the [[ $foo ]] syntax for testing won’t necessarily work. The below will print $foo is not set even though it was set to an empty string:

    foo=""
    if [[ $foo ]]
    then
          echo "\$foo = '$foo'"
    else
          echo "\$foo is not set"
    fi
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written following function which checks whether start_date field is not empty and
I have written a simple jQuery.ajax function which loads a user control from the
I have a function written in C# which has one parameter type as Dictionary<string
I have written a function, which has to check whether a username has been
I have written my own function, which in C would be declared like this,
I have written a function that gets a given number of random records from
I have written a PHP script that checks whether a domain is available for
I've written a function which SHOULD prevent anything from being submitted unless two conditions
I have written a function that positions a tooltip just above a textbox. The
I have written a function below: void trans(double x,double y,double theta,double m,double n) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.