I am having trouble with a short bash script. It seems like all forward slashes needs to be escaped. How can required characters in expanded (environment) variables be escaped before perl reads them? Or some other method that perl understands.
This is what I am trying to do, but this will not work properly.
eval "perl -pi -e 's/$HOME\/_TV_rips\///g'" '*$videoID.info.json'
That is part of a longer script where videoID=$1. (And for some reason perl expands variables both within single and double quotes.)
This simple workaround with no forward slash in the expanded environment variable $USER works. But I would like to not have /Users/ hard coded:
eval "perl -pi -e 's/\/Users\/$USER\/_TV_rips\///g'" '*$videoID.info.json'
This is probably solvable in some better way fetching home dir for files or something else. The goal is to remove the folder name in youtube-dl's json data. I am using perl just because it can handle extended regex. But perl is not required. Any better substitute for extended regex on macOS is welcome.
s{...}{...}instead ofs/.../.../to avoid escaping/(since it is no longer the separator in the statement. You could also use$ENV{HOME}to access the environment variable from Perl instead of using the one from shell. Note that depending on the quoting you might need to escape$then. "And for some reason perl expands variables both within single and double quotes." - Perl isn't expanding anything here but the shell does since you use double quotes around the perl statement, i.e.eval "....shell will expand things here ..."%ENVhash. If you want to escape characters inside a regex, you can just use\Qto quote meta characters. If you add a simple test case, this would probably be an incredibly simple Perl task.