Error echoing a variable to terminal

I am trying to create a service using automator.

Inside a Run Applescript module I have a variable that is a string in which each line is a word separated by linefeed. Like this:

à
às
a
ante
ao
aos
após
aquela

when I try to echo this to terminal by doing:

do shell script "echo " & (finalText as string)

I get this error:

The action “Run AppleScript” encountered an error: “sh: line 1: a: command not found
sh: line 2: à: command not found
sh: line 3: ante: command not found
...
sh: -c: line 30: syntax error near unexpected token `do'
sh: -c: line 30: `do'”

Any ideas?

In an AppleScript, shell commands in a Terminal window are normally run with the do script command. For example:

set aString to "a"

tell application "Terminal"
	do script "echo " & aString
	activate
end tell

The echoed linefeed in the string tells the terminal to run the command. That’s the reason for the errors with:

set aString to "a
b
c"

tell application "Terminal"
	do script "echo " & aString
	activate
end tell

This is avoided by eliminating the linefeeds or by not using the echo command. One possible approach:

set aString to "a
b
c"

tell application "Terminal"
	do script "echo " & quoted form of aString
	activate
end tell

A precise fix requires more information about the script.

Very good! Thanks.