Como fazer backslash de uma string dinâmica no bash

0

Eu quero fazer backslash de uma variável automaticamente para que os usuários finais não precisem digitar as barras invertidas para uma substituição de string de regex Perl.

API_URI="http://something/api"
FIND="(API_URI)(.*?[\=])(.*?[\'](.*?[\']))"
REPLACE="\1\2 \'$API_URI\'"
perl -pi -e "s/${FIND}/${REPLACE}/" file.ext
    
por herkulano 10.01.2017 / 12:46

1 resposta

2

Por todos os meios, use o Perl se você quiser, mas este sed não faz o truque?

echo "$API_URI" | sed 's/\//\\//g'
http:\/\/something\/api

Ou ... em Bash direto:

echo "${API_URI//\//\/}"
http:\/\/something\/api
    
por 10.01.2017 / 13:22