I created shell script, that logs into FTP server and renames some directories. I put FTP commands in multiline string like so

# !/bin/sh

ftp -inv $HOST << EOF

the $COMMANDS contains commands separated by new line. This approach works, when I construct command like so

COMMANDS="command one$NL"
COMMANDS+="command two$NL"
COMMANDS+="command three"

I firstly tried populate $COMMANDS with function printf, but even after several trials, I was unsuccessful. The expression

COMMANDS="$(printf "%s\n" "command one" "command two" "command three")"

evaluated to string with blank spaces between commands and when used in script as FTP command, it was processed as single line.

I tried replace \n with \r which results in variable $COMMANDS not beeing populated with any string.

Can someone more educated please explain to me, whats the problem with printf function here?


I've manage how to achieve my goal via rsync, so the FTP approach I've abandoned, but I'm still curious why this happens.

Problem is with storing printf in variable. I made test script:

echo "==== printf directly"
printf "%s\n" "foo" "bar" "baz"

echo "==== stored in variable"
VAR_1=$(printf "%s\n" "foo" "bar" "baz")
echo $VAR_1

running ./test.sh outputs

==== printf directly
==== stored in variable
foo bar baz

setting IFS=$'\n' does not help.

Update 2

I can get multiline from variable if I wrap it in double qotes

echo "$VAR_1"

but I'm unable to get the same result if used inside heredoc

echo `cat <<EOF

I get output

foo bar baz "foo bar baz"

note: there is even no newline between the two variable outputs in heredoc, which is also strange to me.

migrated from serverfault.com Feb 22 '15 at 21:43

This question came from our site for system and network administrators.

  • 1
    I think you have to mess with IFS (set it to \n only, not leave it to be the default \n or tab or space) to get it right – Dan Feb 21 '15 at 18:19
  • What is your /bin/sh? Is it a symlink to bash? – cuonglm Feb 21 '15 at 18:30
  • 1
    A word of advice: trying to do this with strings, command line ftp, and bash, is unlikely to work even if you get past this hurdle (been there, done that etc.). You should give serious consideration to using 'expect', or using some other language with an FTP library (e.g. python). – Craig Miskell Feb 21 '15 at 19:41
  • Or use a command line FTP client such as lftp – roaima Feb 21 '15 at 20:37
  • for what it's worth, the printf thing works fine here. – wurtel Feb 23 '15 at 9:00

echo eats the newlines when you do not use quotes.
EDIT: See comment, it's not echoed fault.

You already found the improvement

echo "$VAR_1"

Now do the same for the here-document:

echo "`cat <<EOF
  • 4
    It's not echo doing it, the shell does wordsplitting after parameter expansion for command arguments. – jthill Feb 25 '15 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.