String โ
Native String โ
Any unresolved token would be interpreted as string as a final fallback.
name=john_smith # it's a string without quotesRaw String โ
Use single quotes to represent a raw string. All escapes are treated as literal.
script='
cd $(ls ~/projects/* | fzf)
echo hello
'2
3
4
NOTE
You can't interpolate in single quote string
Interpolated String โ
Use double quotes to represent interpolated string.
first_name=john
last_name=smith
fullname="$first_name $last_name"2
3
The best practice is using the complete ${} to distinguish between.
number=123
echo "$number456" # number123 not available in the context
echo "${name}456" # use this instead!2
3
String Concatenation โ
Strings can be concatenated directly side by side except spaces.
first_name=john
last_name=smith
fullname=${first_name}${last_name} # johnsmith #
fullname=${first_name}" "$last_name # john smith #
fullname=$first_name $last_name # error: command not found #2
3
4
5
Here-Doc String โ
Here-Doc is a convenient way to pass string from stdin to target cli. It also preserve indentation just like multi-line raw string. This feature is not specific to string itself but a syntax sugar for passing value without creating subshell.
A Here-Doc uses arbitrary delimiter to scope the string instead of quotes, the common one is EOF.
Interpolated Here-Doc โ
By default, a Here-Doc string can perform interpolation.
python3 << PY
import os
print("${USER}") # USER is interpolated
PY2
3
4
Literal Here-Doc โ
Quoting first delimiter makes the Here-Doc string literally interpreted.
python3 << 'PY'
import os
print(os.environ.get("USER"))
PY2
3
4