bash - In "case $expression in" are double quotes necessary or not? - Stack Overflow

Using bash, in the case esac structure, are double quotes necessary in $expression:case "$express

Using bash, in the case esac structure, are double quotes necessary in $expression:

case "$expression" in
    ...
esac

or not:

case $expression in
    ...
esac

?

Using bash, in the case esac structure, are double quotes necessary in $expression:

case "$expression" in
    ...
esac

or not:

case $expression in
    ...
esac

?

Share Improve this question edited Mar 4 at 14:08 Mario Palumbo asked Mar 4 at 14:00 Mario PalumboMario Palumbo 1,0351 gold badge16 silver badges37 bronze badges 3
  • 1 See the When Should You Quote? section of Quotes - Greg's Wiki. – pjh Commented Mar 4 at 16:53
  • 1 Quotation marks are only necessary if $expression contains spaces or special characters. In general, they are recommended because future changes to $expression will still make the statement work. – Wiimm Commented Mar 4 at 20:16
  • 2 You're thinking about this backwards - code defensively and think of quotes as something you remove when necessary, not something you add when necessary. Always quote your variables by default unless you have a need for word splitting, etc. and then remove them - you will shoot yourself in the foot far less often with that approach than trying to figure out every time you use a variable whether or not you need to add them. – Ed Morton Commented Mar 5 at 0:58
Add a comment  | 

1 Answer 1

Reset to default 5

No, quotes are generally not necessary.

According to the manual (see Conditional Constructs):

The word undergoes tilde expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal (see Shell Parameter Expansion) before matching is attempted.

Since pathname expansion and word splitting are not applied to word, the typical function of quotes to suppress those behaviors is redundant. The only potential benefit of quotes is to suppress tilde expansion.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745039683a4607736.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信