How do I escape an exclamation mark in bash?

后端 未结 4 717

Sometimes I feel the urge to put some more expressiveness in my git commit messages. Unfortunately bash does not seem to like this.

iblue@silence ~/git/warga         


        
相关标签:
4条回答
  • 2020-12-06 17:47

    In addition to using single quotes for exclamations, in most shells you can also use a backslash \ to escape it. That is: git commit -m "Frustrating <insert object of frustration here>\!"

    However, I personally recommend disabling bash expansion in you shell by adding set +H or set +o histexpand to your .bashrc file.

    If, like me, you never use bash expansion in your shell, disabling it will allow you to use exclamation points in any double-quote string - not only during your commits but for all bash commands.

    0 讨论(0)
  • 2020-12-06 17:54

    Exclamation mark is preserved literally when you include it in a single-quoted string.

    Example:

    git commit -m 'Frustrating <insert object of frustration here>!'
    
    0 讨论(0)
  • 2020-12-06 17:57

    Have a try this one

    git commit -m "Frustrating <insert object of frustration here>"'!'

    If in the middle of string then

    "hello"'!'"world"

    0 讨论(0)
  • 2020-12-06 17:57

    Use single quotes instead to prevent expansion.

    0 讨论(0)
提交回复
热议问题