Newline in a for loop
Solution 1
In the line:
echo -e "\nBackup of ${i%%/} successfull" >> $logs/backups.log
Replace the Newline (\n
) with a Carriage Return + Newline (\r\n
) to ensure that both are happening.
For more info, perhaps check out this Programmers.stackexchange.com question: Difference between '\n' and '\r\n', and this StackOverflow question: Historical reason behind different line ending at different platforms
Solution 2
You can use CTRL
+ V
together then press J
to embed a newline(s) in the echo statement.
On Unix and Linux systems CTRL + V
indicates that the character that follows is interpreted as a control character.
Related videos on Youtube
Alexander Kim
Middle Frontend-engineer. Working with JS: Vue/Nativescript/Electron/Express || Koa.
Updated on September 18, 2022Comments
-
Alexander Kim over 1 year
How can i make a newline in a for loop in bash? I've tried
echo -e
but its not working. When i am opening a log file everything is on a single line.for i in */ ; do sitefolder="$HOME/domains/${i%%/}/public_html" if [ -d "$sitefolder" ]; then echo -e "\nBackup of ${i%%/} successfull" >> $logs/backups.log fi done
What am i doing wrong? Thanks
-
Ƭᴇcʜιᴇ007 over 10 yearsNot sure, but try
\r\n
(Carriage Return + Newline) instead of just\n
(Newline). -
Alexander Kim over 10 yearsI will try in a few mins :) will tell you if it worked, thanks
-
-
Alexander Kim over 10 yearsI have to use Vi editor to make that?
-
suspectus over 10 yearsnot necessarily - it's not vi specific. You can do the same in bash.
-
Alexander Kim over 10 yearsWell i am editing my script.sh file using PhpStorm. How can i do that there? :)
-
suspectus over 10 yearsDon't know about phpStorm - you need to embed a CTRL+J character though.
-
suspectus over 10 yearsbash on windows so you using cygwin?
-
suspectus over 10 yearsok. Windows is not relevant here. You are logged onto a linux machine. Two possibilities are: use vi to embed the newline, or look up help for phpStorm editor to find how to embed CTRL+j.