最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论