Vicky Dev
Vicky Dev

Reputation: 2173

Bash/GitBash: Regex comparison doesn't work inside aliases

As stated in Q-title, I have an alias setup for running multiple Git commands if the current directory in GitBash terminal is valid Git repository.

Now I am trying to do a regex comparison of User prompt's answer such that if User types y or Y then it must go ahead and run the command after the next && like below:

alias abc="([ `git rev-parse --git-dir` == \".git\" ] && git restore \"*lock*\" \"*mix-manifest*\" && git status && (read -p \"Run 'git diff'? \" && [[ $REPLY =~ ^[Yy]$ ]] && git diff))"

But this alias when called throws the following error:

bash: conditional binary operator expected
bash: syntax error near `^[Yy]$'

And from this bit-vague error message, I got that the problem is somewhere in [[ $REPLY =~ ^[Yy]$ ]] and maybe because I am using this comparison inside double-quotes of an alias. I already tried replacing ^[Yy]$ with '^[Yy]$' and \"^[Yy]$\", but in vain.

Can anyone assist in figuring out the problem in [[ $REPLY =~ ^[Yy]$ ]] and fix this ?

Upvotes: 0

Views: 63

Answers (1)

Vicky Dev
Vicky Dev

Reputation: 2173

With the help of nice suggestion by @tjm3772, I was able to fix this by converting the alias into function as below:

abc(){ ([ `git rev-parse --git-dir` == ".git" ] && git restore "*lock*" "*mix-manifest*" && git status && (read -p "Run 'git diff'? " && [[ $REPLY =~ ^[Yy]$ ]] && git diff)); }

And declaring this in .bashrc file, and it worked like charm.

Upvotes: 1

Related Questions