Zhanghua
Zhanghua

Reputation: 1

How to use echo to wrap a variable with quotes

I have a variable e.g., pos=11111.

I want

echo "$pos", which displays

"11111", instead of 11111.

I tried to use

echo "\"$pos\""

but it missed the last quote as shown:

"11111

Is their any solution for this?

Upvotes: 0

Views: 110

Answers (1)

Eric Renouf
Eric Renouf

Reputation: 14490

I'd just use printf then:

printf '"%s"' "$pos"

and if you want the newline:

printf '"%s"\n' "$pos"

printf is better than echo anyway

In fact the POSIX documentation for echo even says:

New applications are encouraged to use printf instead of echo.

Upvotes: 5

Related Questions