0

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,030
  • 21
  • 100
  • 124
neo33
  • 1,699
  • 5
  • 16
  • 38

2 Answers2

7

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,030
  • 21
  • 100
  • 124
Dirk Herrmann
  • 5,101
  • 1
  • 18
  • 44
5

With GNU Bash:

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

Output:

this is a "number"
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Cyrus
  • 77,979
  • 13
  • 71
  • 125
  • 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