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 92133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:08:21+00:00 2026-05-10T23:08:21+00:00

I’ve got a few Unix shell scripts where I need to check that certain

  • 0

I’ve got a few Unix shell scripts where I need to check that certain environment variables are set before I start doing stuff, so I do this sort of thing:

if [ -z '$STATE' ]; then     echo 'Need to set STATE'     exit 1 fi    if [ -z '$DEST' ]; then     echo 'Need to set DEST'     exit 1 fi 

which is a lot of typing. Is there a more elegant idiom for checking that a set of environment variables is set?

EDIT: I should mention that these variables have no meaningful default value – the script should error out if any are unset.

  • 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. 2026-05-10T23:08:21+00:00Added an answer on May 10, 2026 at 11:08 pm

    Parameter Expansion

    The obvious answer is to use one of the special forms of parameter expansion:

    : ${STATE?"Need to set STATE"} : ${DEST:?"Need to set DEST non-empty"} 

    Or, better (see section on ‘Position of double quotes’ below):

    : "${STATE?Need to set STATE}" : "${DEST:?Need to set DEST non-empty}" 

    The first variant (using just ?) requires STATE to be set, but STATE="" (an empty string) is OK — not exactly what you want, but the alternative and older notation.

    The second variant (using :?) requires DEST to be set and non-empty.

    If you supply no message, the shell provides a default message.

    The ${var?} construct is portable back to Version 7 UNIX and the Bourne Shell (1978 or thereabouts). The ${var:?} construct is slightly more recent: I think it was in System III UNIX circa 1981, but it may have been in PWB UNIX before that. It is therefore in the Korn Shell, and in the POSIX shells, including specifically Bash.

    It is usually documented in the shell’s man page in a section called Parameter Expansion. For example, the bash manual says:

    ${parameter:?word} 

    Display Error if Null or Unset. If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.

    The Colon Command

    I should probably add that the colon command simply has its arguments evaluated and then succeeds. It is the original shell comment notation (before ‘#‘ to end of line). For a long time, Bourne shell scripts had a colon as the first character. The C Shell would read a script and use the first character to determine whether it was for the C Shell (a ‘#‘ hash) or the Bourne shell (a ‘:‘ colon). Then the kernel got in on the act and added support for ‘#!/path/to/program‘ and the Bourne shell got ‘#‘ comments, and the colon convention went by the wayside. But if you come across a script that starts with a colon, now you will know why.


    Position of double quotes

    blong asked in a comment:

    Any thoughts on this discussion? https://github.com/koalaman/shellcheck/issues/380#issuecomment-145872749

    The gist of the discussion is:

    … However, when I shellcheck it (with version 0.4.1), I get this message:

    In script.sh line 13: : ${FOO:?"The environment variable 'FOO' must be set and non-empty"}   ^-- SC2086: Double quote to prevent globbing and word splitting. 

    Any advice on what I should do in this case?

    The short answer is "do as shellcheck suggests":

    : "${STATE?Need to set STATE}" : "${DEST:?Need to set DEST non-empty}" 

    To illustrate why, study the following. Note that the : command doesn’t echo its arguments (but the shell does evaluate the arguments). We want to see the arguments, so the code below uses printf "%s\n" in place of :.

    $ mkdir junk $ cd junk $ > abc $ > def $ > ghi $  $ x="*" $ printf "%s\n" ${x:?You must set x}    # Careless; not recommended abc def ghi $ unset x $ printf "%s\n" ${x:?You must set x}    # Careless; not recommended bash: x: You must set x $ printf "%s\n" "${x:?You must set x}"  # Careful: should be used bash: x: You must set x $ x="*" $ printf "%s\n" "${x:?You must set x}"  # Careful: should be used * $ printf "%s\n" ${x:?"You must set x"}  # Not quite careful enough abc def ghi $ x= $ printf "%s\n" ${x:?"You must set x"}  # Not quite careful enough bash: x: You must set x $ unset x $ printf "%s\n" ${x:?"You must set x"}  # Not quite careful enough bash: x: You must set x $  

    Note how the value in $x is expanded to first * and then a list of file names when the overall expression is not in double quotes. This is what shellcheck is recommending should be fixed. I have not verified that it doesn’t object to the form where the expression is enclosed in double quotes, but it is a reasonable assumption that it would be OK.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 107k
  • Answers 108k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer When you create the persistent store coordinator, if you are… May 11, 2026 at 9:09 pm
  • Editorial Team
    Editorial Team added an answer Yep, works fine. Make sure that you install it before… May 11, 2026 at 9:09 pm
  • Editorial Team
    Editorial Team added an answer As long as the classes you want to load are… May 11, 2026 at 9:09 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.