BASH_REMATCH does not commit
I am trying to grab part of a path in bash:
Entrance: /Users/foo/.virtualenvs/venv-test-server
code:
#!/bin/zsh
regex="^.*\/venv-(.*)$"
if [[ $VIRTUAL_ENV =~ $regex ]] ; then
echo "Matched!"
echo ${BASH_REMATCH[1]}
fi
Output: Matched!
But the match is not printed. Why?
+3
melbic
source
to share
2 answers
The script indicates zsh
instead of bash
:
#!/bin/bash
^^^^
If you want to use zsh
, before using =~
, you need to install BASH_REMATCH
:
setopt KSH_ARRAYS BASH_REMATCH
+5
falsetru
source
to share
The equivalent array in zsh
is match
:
% [[ foo_bar =~ (.*)_(.*) ]]
% print $match[1]
foo
% print $match[2]
bar
+3
chepner
source
to share