Add a space

Submitted by:Jhon Jhon

Date added:18 June, 2011

Category:Bash

Add a space (or other delimiter) only if a variable is set, in order to avoid ugly unnecessary spaces.

Tags: add space , avoid unnecessary spaces

Code Snippet:

$ first=Joe
$ last= # last name blank, the following echoes a space before the period
$ echo "Hello, $first $last. Welcome to..."
Hello, Joe . Welcome to...

$ echo "Hello, $first${last:+ $last}. Welcome to..."
Hello, Joe. Welcome to...

$ last=Green
$ echo "Hello, $first${last:+ $last}. Welcome to..."
Hello, Joe Green. Welcome to...
 
 

Comments