Pacific-Design.com

    
Home Index

1. Bash

2. Escape String

Bash / Escape String /

Escape backslashes within a string.


#!/bin/bash
# How to escape and replace string path in file

# escape string to \/home\/kevin\/data
BASE='/home/kevin/data'
ESCAPED=`echo $BASE | sed -e 's/\//\\\\\//g'`

# replace escaped string using sed
echo "ESCAPED="$ESCAPED
sed -i "s/path/$ESCAPED/" data_file.txt

Escape Single Quote


sed -i "s/'/\\\\\'/g" names.dat