For diverse reasons I'd like sometimes to have a string with newlines in a shell variable. For example, I could keep some short scripts (like FTP commands) built during the runtime of my script and simply feed them to the "ftp" command like:

Code:

echo "$script" | ftp -n ftp.myhost.pl

That is the theory, the practical problem is that executing "echo $script" strips the $script variable of all the newlines.

Of course I could use "\n"-s and execute "echo" with an "-e" parameter in this case. Sometimes however I'd like to do some more processing on the variable but and one execution of "echo" feeds it correctly but also strips it of all the "\n"-s and the next time becomes a bit tricky.

Is there some alternative to "echo" I could use to pass variables to streams that would preserve the original newlines?

As for ~/.netrc, I can automate logging in and write some simple FTP macros here, that's not always enough. Not to mention, I prefer to keep my projects in single places and not to spread them all over the server. And anyway, the FTP here was only an example; I encountered the need to keep a text with newlines in a variable several times in pretty diverse conditions.

@Watael

Thanks, I know the heredoc syntax and use it a lot, but that's not always a solution. Sometimes you need to build an FTP script or a MySQL script or something line by line during the execution of a bash script and every line depends on different conditions. Keeping the script beeing built in a variable seems reasonable in such a case. But having solved the newline problem, the only thing that puzzle me as a shell programmer now is what are the limits of the text I can keep in a variable...

Sorry! You better be careful !Personally I only use vi(m) as editor for bash scripting and came not across this problem up to now.To be honest, I seldom use here-documents. But when I do, I prefer them to be nicely integrated into the script.

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum