Sam
Sam

Reputation: 2331

Arguments to printf are being read as separate commands in spite of a backslash in the end

I put two printf commands in this script that seem alsmost identical to me. The first command is printing the output to a text file as it should. The second command should be doing the same thing to another textfile but it prints to standard output and then prints some errors. I can't notice the difference between the two

This is the script I'm using

printf "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n" "test1" \
                 "test2" \
                 "test3" \
                 "test4" \
                 "test5" \
                 "test6" \
                 "test7" \
                 "test8" \
                 "test9" > testfile.txt

printf "%s\n%s\n%s\n%s\n%s\n" "testa" \ 
        "testb" \ 
        "testc" \
        "testd" \
        "teste" > testfile2.txt

And this is the output

testa




./script.txt: line 12: testb: command not found
./script.txt: line 14: testc: command not found

testfile.txt has this

test1
test2
test3
test4
test5
test6
test7
test8
test9

testfile2.txt has nothing

Upvotes: 0

Views: 49

Answers (1)

codeforester
codeforester

Reputation: 42999

The shell continuation character \ should always be the last character before the newline. If you have trailing spaces, they would nullify the effect of backslash and the next line is treated as an independent command by shell.

Upvotes: 3

Related Questions