bash variable in string code example

Example 1: combine strings bash

foo="Hello"
foo="${foo} World"
echo "${foo}"
> Hello World

Example 2: bash command in variable

#Just use following structure to store output of command into a variable:
var=$(command)
#For example:
var=$(echo 'hi')	#store hi into var
var=$(ls)	#store list of files into var

Example 3: sh concat string

VAR1="Hello,"
VAR2=" World"
VAR3="$VAR1$VAR2"
echo "$VAR3"

Example 4: bash use variable in string

domain='http://www.whitehouse.gov'
path='/some/path'
base_url="$domain$path"

Example 5: bash variable in string

${!var}	#Just use to use reference value inside another variable ;)

Tags:

Misc Example