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 More Than Just a Constant String for Default in bash
Bash

Using More Than Just a Constant String for Default in bash

InfinityCoder February 14, 2017

You need something more than just a constant string as the default value for the variable.

You can use quite a bit more on the righthand side of these shell variable references.
For example:

1
cd ${BASE:="$(pwd)"}

As the example shows, the value that will be substituted doesn’t have to be just a string constant.

Rather it can be the result of a more complex shell expression, including running commands in a subshell (as in the example).

In our example, if $BASE is not set, the shell will run the pwd built-in command (to get the current directory)
and use the string that it returns as the value.
So what can you do on the righthand side of this (and the other similar) operators?

The bash manpage says that what we put to the right of the operator “is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.”
Here is what that means:

• Parameter expansion means that we could use other shell variables in this expression, as in: ${BASE:=${HOME}}.
• Tilde expansion means that we can use expressions like ~bob and it will expand that to refer to the home directory of the username bob. Use ${BASE:=~uid17} to set the default value to the home directory for user uid17, but don’t put quotes around this string, as that will defeat the tilde expansion.
• Command substitution is what we used in the example; it will run the commands and take their output as the value for the variable. Commands are enclosed in the single parentheses syntax, $( cmds ).
• Arithmetic expansion means that we can do integer arithmetic, using the $(( … )) syntax in this expression.

Here’s an example:

1
echo ${BASE:=/home/uid$((ID+1))}

 

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Running Long Jobs Unattended in bash
You ran a job in the background, then exited the …

Running Long Jobs Unattended in bash

Rewrapping Paragraphs in bash
You have some text with lines that are too long …

Rewrapping Paragraphs 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