1

I want to escape double quotes in Bash. I followed the following approach:

#!/bin/bash
this is a  \"number\"!

But is there another way?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
neo33
  • 1,809
  • 5
  • 18
  • 41

2 Answers2

9

You can enclose the double quotes in single quotes:

echo '"'hola'"'

Or alternatively the whole text, including the double quotes:

echo '"hola"'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dirk Herrmann
  • 5,550
  • 1
  • 21
  • 47
5

With GNU Bash:

echo -e "this is a \x22number\x22"

Output:

this is a "number"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • 2
    Better yet, `printf "this is a \42number\42\n"`, which will work in any POSIX-compatible shell. (`\42` is the character whose ASCII value is 42 in *octal*.) – chepner Feb 23 '16 at 23:32