Quantcast
Channel: how to express line feed in shell? - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 4

Answer by Gilles for how to express line feed in shell?

$
0
0

Just include an actual newline inside the quotes (this works with either single or double quotes). Note that if the second line is indented, the spaces are part of the string. Furthermore always use double quotes around variable substitutions.

str="deb http://ftp.cn.debian.org/debian/ wheezy main contrib non-free
deb-src http://ftp.cn.debian.org/debian/ wheezy main contrib non-free"
echo "$str" > test

Ksh, bash and zsh (but not plain sh) have an alternate quoting syntax $'…' in which backslash begins a C-like escape sequence, so you can write \n to represent a newline. In your case, it would be less readable:

str=$'deb http://ftp.cn.debian.org/debian/ wheezy main contrib non-free\ndeb-src http://ftp.cn.debian.org/debian/ wheezy main contrib non-free'
echo "$str" > test

A here document is often a more readable way to present multiline strings. Note that a here document is passed as standard input to the command¹, not as a command line argument.

cat <<EOF >test
deb http://ftp.cn.debian.org/debian/ wheezy main contrib non-free
deb-src http://ftp.cn.debian.org/debian/ wheezy main contrib non-free
EOF

¹ Or more generally as input if you specify an alternate file descriptor.


Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>