InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Bash shell scripting

  • Bash - add a number to a variable
  • Bash - append text to a variable
  • Bash - how to check if a variable is set
  • Bash - how to compare file timestamps
  • Bash - how to find last command exit status code
  • Bash - how to get main program and current file dir location
  • Bash - how to redirect stderr to stdout or file
  • Bash - how to run custom commands at script exit
  • Bash - how to stop at error
  • Bash - how to use functions - quick tutorial
  • Bash - iterate over array
  • Bash - local and global variables
  • Bash - newline and other escape character in string
  • Bash - pass all arguments from one script to another
  • Bash - set default value if a variable is empty
  • Bash - variables in double quotes vs without quotes
  • Bash associative array tutorial
  • Bash check if file begins with a string
  • Bash shell - check if file or directory exists
  • Can global variables be modified in bash function?
  • Find memcache request hit rate on linux command line
  • How to return a value from bash function
  • Iterate over specific file extension in a dir in shell script
  • Linux - Yesterday's Date in YYYYMMDD format
  • bash - extract urls from xml sitemap
  • bash - how to use regex in if condition
 
  • Home
  • > Tutorials
  • > Bash shell scripting

Bash – how to check if a variable is set

By admin on Dec 17, 2015

To check if a variable is set or empty, we can use couple of approaches. One approach is to use empty string check (if [ -z $string]) . This approach is little inaccurate but will work in most cases. Better approach is to use parameter substitution (${var+val}) and then empty string check (if [ -z $string]).

Parameter substitution using ${var+val}

If variable var is set then use val otherwise null.

#!/bin/bash
var=somethings
echo ${var+val}
val
Env: GNU bash, version 4.2.46

Bash code – check if a variable is set

Here empty string will be considered as set.

#!/bin/bash
if [ -z ${var+x} ]; then
  echo "variable var is not set"
else
  echo "variable var is set"
fi

var=""
if [ -z ${var+x} ]; then
  echo "variable var is not set"
else
  echo "variable var is set"
fi
variable var is not set
variable var is set
Env: GNU bash, version 4.2.46
Note that in ${var+x} we can use any string in place of x.

Suggested posts:

  1. Can ssh keys generated as one user be used as a different user
  2. Bash – append text to a variable
  3. Why and how to use Amazon route53 as DNS
  4. Bash – newline and other escape character in string
  5. Bash shell – check if file or directory exists
  6. How to specify environment variable for a command on Linux
  7. How to show environment variable for a process id (pid)
  8. Jquery – change table cells color based on value
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Bash shell scripting, Linux, Linux Interview Questions, Mac, Tutorials, Ubuntu Linux
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress