Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Short way to escape HTML in Bash?

The box has no Ruby/Python/Perl etc.

Only bash, sed, and awk.

A way is to replace chars by map, but it becomes tedious.

Perhaps some built-in functionality i'm not aware of?

like image 532
James Evans Avatar asked Oct 13 '12 13:10

James Evans


People also ask

How do I escape HTML code?

Escape characters will always begin with the ampersand symbol (&) and end with a semicolon symbol (;). The characters in between the ampersand and semicolon make up the specific code name or number for a particular character.

How do you escape a command in bash?

There are many methods to quit the bash script, i.e., quit while writing a bash script, while execution, or at run time. One of the many known methods to exit a bash script while writing is the simple shortcut key, i.e., “Ctrl+X”. While at run time, you can exit the code using “Ctrl+Z”.


2 Answers

Escaping HTML really just involves replacing three characters: <, >, and &. For extra points, you can also replace " and '. So, it's not a long sed script:

sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g; s/"/\&quot;/g; s/'"'"'/\&#39;/g' 
like image 156
ruakh Avatar answered Sep 22 '22 03:09

ruakh


You can use recode utility:

    echo 'He said: "Not sure that - 2<1"' | recode ascii..html 

Output:

    He said: &quot;Not sure that - 2&lt;1&quot; 
like image 28
Ivan Avatar answered Sep 22 '22 03:09

Ivan