Quantcast
Viewing latest article 2
Browse Latest Browse All 4

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

One option is to use echo -e to expand the escape sequences. The second option is to simply use a "literal" newline (works in bash):

str = "deb ... non-free  "$'\n'"deb-src ... non-free  "
echo "$str"

Note the $'···' notation to insert a literal.

However, having newlines in variables like this is not a good idea. It's harder to read the script, it could lead to silly mistakes and unwanted behaviour if "$str" is given to other programs that either don't understand escape sequences (if \n is used), or uses word splitting ($'' case). Just use an array and iterate over it, it will make it more extensible if you have more lines.

If you just want this in one place in the code, I'd split it into two echo commands, that at least can't go wrong.

Another interesting and probably the best solution if you just want to put it into a file, is a here-document:

cat > test <<EOF
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

Further reading: https://stackoverflow.com/questions/9139401/trying-to-embed-newline-in-a-variable-in-bash


Viewing latest article 2
Browse Latest Browse All 4

Trending Articles