How to print backslash in shell script using awk?

I found that

echo "aaa" | awk '{print ",\\";}'

works, and it will give "\".

but

ddd=`echo "aaa" | awk '{print ",\\";}'`; echo $ddd

will not work.
Could anyone tell me why? thank you.

ddd=$( echo "aaa" | awk '{print ",\\";}' )
echo $ddd

it works, Thank you.

You can use BEGIN to make awk work without no input

ddd=$(awk 'BEGIN {print ",\\";}' )
$ ddd=",\\"; echo $ddd
,\

UUOA?

ddd=,\\

UUOQ&A :wink:

OP never gave a complete picture of what he was trying to achieve. Thread title is just: "How to print backslash in shell script using awk?"

So I'm not sure if it was UUOQ & UUOA

Smile. Your correction proposal was correct. Backticks make escaping difficult.

I think it will work but with a minor modification:

ddd=`echo "aaa" | awk '{print ",\\\\";}'`