Bash – How shall I pass a newline character to a command in a script

bashscripting

I have a bash script, which concatenate its arguments by a given separator

#! /bin/bash   

d="$1";
shift;

echo -n "$1";
shift;
printf "%s" "${@/#/$d}";

This is how I use it:

$ a=(one two 'three four' five)
$ myscript ' *** '  "${a[@]}" 
one *** two *** three four *** five

Now I would like to make a newline as the separator, but that doesn't happen:

$ myscript '\n'  "${a[@]}" 
one\ntwo\nthree four\nfive

How shall I pass a newline character to the printf command in the script? (I am not looking for rewriting my script, if that is possible).
Thanks.

Best Answer

Use the $'...' kind of quotes, if you want the \n to be expanded into a newline character:

myscript $'\n'  "${a[@]}"

Or pass the newline literally inside single or double quotes:

myscript '
'  "${a[@]}"
Related Question