Search code examples
javascriptbashescapingecho

Escaping special character bash


Im writing a bash file to automate some of the setup for creating a webpage. The following piece of code is used as part of creating a node server.

echo const server = http.createServer((req, res) => { >> index.js

The arrow part of the code messes up the output since the > character is used for redirection. My index.js file ends up looking like this:

const server = http.createServer((req, res) =

I have tried to escape the special character using a backslash and i've tried using single quotes to turn it into a string. Like this:

echo const server = http.createServer((req, res) =\> { >> index.js
'echo const server = http.createServer((req, res) => { >> index.js'

However this simply produces the following in my index.js:

const server = http.createServer((req, res) =\
'const server = http.createServer((req, res) =

Solution

  • You can use "..." for strings.

    echo "const server = http.createServer((req, res) => {" >> index.js
    

    does the job.