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.