jay
jay

Reputation: 503

Echoing an echo option

Today I was testing a script and for some reason I needed to print -n to the console.

I tried echo -n, which of course just echos without a new line. I tried echo "-n", which does the same.

Then I tried assigning to a variable: str="-n". Of course echo $str and echo "$str" do nothing.

How would you echo exactly -n?

Upvotes: 0

Views: 2225

Answers (2)

BirdBirdBird
BirdBirdBird

Reputation: 1

You could escape (\) both of those characters, telling your shell to read them both as literal characters.

Running echo \-\n gives me -n as output.

Upvotes: 0

Barmar
Barmar

Reputation: 780861

Use printf rather than echo

printf '%s\n' '-n'

Upvotes: 5

Related Questions