InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Using null As a Valid Default Value in bash
Bash

Using null As a Valid Default Value in bash

InfinityCoder February 14, 2017

You need to set a default value, but you want to allow an empty string as a valid value.

You only want to substitute the default in the case where the value is unset.
The ${:=} operator has two cases where the new value will be used: first, when the value of the shell variable has previously not been set (or has been explicitly unset); and second, where the value has been set but is empty, as in HOME=”” or HOME=$OTHER (where $OTHER had no value).

The shell can distinguish between these two cases, and omitting the colon (:) indicates that you want to make the substitution only if the value is unset.

If you write only ${HOME=/tmp} without the colon, the assignment will take place only in the case where the variable is not set (never set or explicitly unset).

Let’s play with the $HOME variable again, but this time without the colon in the operator:

1
2
3
4
5
6
7
8
9
10
11
$ echo ${HOME=/tmp} # no substitution needed
/home/uid002
$ HOME=""           # generally not wise
$ echo ${HOME=/tmp} # will NOT substitute
 
$ unset HOME        # generally not wise
$ echo ${HOME=/tmp} # will substitute
/tmp
$ echo $HOME
/tmp
$

In the case where we simply made the $HOME variable an empty string, the = operator didn’t do the substitution since $HOME did have a value, albeit null.

But when we unset the variable, the substitution occurs.

If you want to allow for empty strings, use just the = with no colon.

Most times, though, the := is used because you can do little with an empty value, deliberate or not.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Finding Files by Date in bash
Suppose someone sent you a JPEG image file that you …

Finding Files by Date in bash

Separating Variable Names from Surrounding Text in bash
You need to print a variable along with other text. …

Separating Variable Names from Surrounding Text in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap