Bash – newline and other escape character in string

To have newline (\n) and other escape characters in bash strings, we can use $'string' syntax (ANSI-C Quoting). $'string' expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Here is an example.

#!/bin/bash
v=$'a\nb'
echo "$v"
## echo directly
echo $'aa\nbb'
echo $'aaa\tbbb'
a
b
aa
bb
aaa	bbb
Env: GNU bash, version 4.3.11

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments