in reply to passing newline in string argument

You didn't pass a Line Feed. You passed a Backslash and a Latin Small Letter N

To Pass a Line Feed, you can use

# tcsh -string 'foo\ bar'
or
# POSIX sh, ksh, bash, dash -string 'foo bar'
or
# POSIX sh, ksh, bash, dash -string "$( printf 'foo\nbar' )"
or
# bash -string $'foo\nbar'

Is there a way to have the '\n' interpreted as an actual newline character by the perl script?

s/\\n/\n/g

But what if someone actually wants to provide the two characters \n? It's up to you to define your language and write a parser and interpreter for it.

And don't forget that scripts that call your program would need an escaping function to provide user input to your program.

It's far simpler to simply provide the correct string, which is why I led with that. Is there a way to have the '\n' interpreted as an actual newline character by the perl script?

Replies are listed 'Best First'.
Re^2: passing newline in string argument
by haukex (Archbishop) on Aug 02, 2025 at 08:40 UTC
    But what if someone actually wants to provide the two characters \n?

    Special_K, just for completeness, here's one common way - however, as we both said, this is only a workaround with its own caveats, and the problem should be fixed in the shell instead!