在bash中将bash中的方括号转义为if [英] Escaping square bracket in bash inside a string for an if

查看:104
本文介绍了在bash中将bash中的方括号转义为if的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个简单的 if 语句,但是其中一个字符串包含方括号。

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.

我基本上是这样:

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

,在这种情况下,我想抛出一条错误消息。

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

推荐答案

也引用$ MESSAGE。如果该变量确实包含[,它将影响解析。

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

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

这篇关于在bash中将bash中的方括号转义为if的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆