Escaping square bracket in bash inside a string for an if

六眼飞鱼酱① 提交于 2019-12-02 02:31:04

问题


I'm trying to do a simple if statement but one of the string contains square brackets.

I've tried with \ ' " + and everything came in my mind.

I've basically this:

if [ $MESSAGE = "username [$USERNAME] is already taken" ]

and in this case I would like to throw an error message.


回答1:


Quote $MESSAGE as well. If that variable does contain a [, it will affect the parsing.

if [ "$MESSAGE" = "username [$USERNAME] is already taken" ]


来源:https://stackoverflow.com/questions/11419668/escaping-square-bracket-in-bash-inside-a-string-for-an-if

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!