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
Setting Default Values in bash
Bash

Setting Default Values in bash

InfinityCoder February 14, 2017

Your script may rely on certain environment variables, either widely used ones (e.g., $USER) or ones specific to your own business.

If you want to build a robust shell script, you should make sure that these variables do have a reasonable value.

You want to guarantee a reasonable default value. How?

Use the assignment operator in the shell variable reference the first time you refer to it to assign a value to the variable if it doesn’t already have one, as in:

1
cd ${HOME:=/tmp}

The reference to $HOME in the example above will return the current value of $HOME unless it is empty or not set at all.

In those cases (empty or not set), it will return the value /tmp, which will also be assigned to $HOME so that further references to $HOME will have this new value.

We can see this in action here:

1
2
3
4
5
6
7
8
9
10
$ echo ${HOME:=/tmp}
/home/uid002
$ unset HOME # generally not wise to do
$ echo ${HOME:=/tmp}
/tmp
$ echo $HOME
/tmp
$ cd ; pwd
/tmp
$

Once we unset the variable it no longer had any value. When we then used the := operator as part of our reference to it, the new value (/tmp) was substituted.

The subsequent references to $HOME returned its new value.
One important exception to keep in mind about the assignment operator: this mechanism will not work with positional parameter arguments (e.g., $1 or $*).

For those cases, use :- in expressions like ${1:-default}, which will return the value without trying to do the assignment.
As an aside, it might help you to remember some of these crazy symbols if you think of the visual difference between ${VAR:=value} and ${VAR:-value}.

The := will do an assignment as well as return the value on the right of the operator.

The :- will do half of that—it just returns the value but doesn’t do the assignment—so its symbol is only half of an equal sign (i.e., one horizontal bar, not two).

If this doesn’t help, forget that we mentioned it.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Cleaning Up an SVN Source Tree in bash
Subversion’s svn status command shows all the files that have …

Cleaning Up an SVN Source Tree in bash

Creating a Command-Line Calculator in bash
You need more than just integer arithmetic, and you’ve never …

Creating a Command-Line Calculator 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