I'm writing a name-helping script to automatically set the "name": field in a package.json file so that it matches a certain regex structure, but I'm having some issues actually setting the name. The regex it must match is '\@abc\/([a-z]+-{0,1})+[a-z]*$'.
Right now I do basically this (along with some extra stuff to really assert that the naming convention is followed):
pattern='\@abc\/([a-z]+-{0,1})+[a-z]*$'
if [[ ! $name =~ $pattern ]]; then
read -rp "New name: " newName
sed -ri "s/(\s.\"name\"\:\s\").*/\1$newName\",/g" $1/package.json
fi
As you might see, the problem here is that the variable $newName gets processed in sed as a command, it needs to be escape charactered (assuming that the user actually wrote in a new name with correct structure). Is there a way to do this? Preferably as dependency un-reliant as possible.
jqthan withsed. – Kusalananda Jul 28 '21 at 14:51@abcor\@abc? The@isn't special in any regular expression flavor I am familiar with, so a\@will be interpreted as a@. If you want to match a literal backslash, you need\\@. – terdon Jul 28 '21 at 14:51s/@example.org/b/will try to interpolate array @example into the LHS of thes//operator.s/\@a/b/won't, it will be just a literal @. This is important to remember when trying to match email addresses; and b) there are several cases in vim regexes where @ has a different meaning depending on whether it's escaped or not. – cas Jul 29 '21 at 05:20