For a string in this format:./my-site.sub.domain.comI would like to remove the .,-, and /, and trim the remainder to 16 characters: mysitesubdomaincI've done thismysite=./my-site.sub.domain.commysite=${mysite//[\.|\/|-]//}mysite=${mysite:0:16}echo $mysitemysitesubdomaincIs there a way to combine the replacement and sub-string extraction?
Bash Combine Replacement and Sub String Extraction in One Step
bash;shell script;regular expression;string
There's no way to chain the Bash built-in parameter expansion, but of course this can be done in a single line with external tools like sed:$ sed 's/[\.\/-]//g;s/^\(.\{16\}\).*/\1/' <<< ./my-site.sub.domain.commysitesubdomaincUnfortunately this very quickly turns into unmaintainable code, and is probably less efficient than using Bash internals, so I would advise against it.