zsh - Enter a string containing "!!" on a shell without it being interpretted

07
2014-07
  • David

    I'm trying to send a string that contains "!!" in it. However, every time I execute it on zsh, zsh replaces !! with the last command entered. How can I get zsh to not interpret "!!"? I have also confirmed the same thing happens when I just use sh.

  • Answers
  • bbbco

    Have you tried quoting your string with single quotes instead of double quotes?

    https://www.linuxquestions.org/questions/programming-9/bash-double-quotes-don%27t-protect-exclamation-marks-545662/

    bbbco (04:45 PM) ~/ $ echo "Exclamation!!"
    echo "Exclamationecho "Exclamationecho 'Exclamation!!'""
    Exclamationecho Exclamationecho Exclamation!!
    
    bbbco (04:43 PM) ~/ $ echo 'Exclamation!!'
    Exclamation!!
    
  • pbies

    Have you tried one backslash for every character?

    \!\!
    

    Preceding characters with backslash (one backslash per one character) should give in effect exactly this character, not replaced (it is not interpreted then).

  • mpy

    As mentioned by others, quoting or escaping helps to prevent the history expansion in the current command line.

    But, you can also use

    unsetopt BANG_HIST
    

    to disable the special treatment the character ! completely. (Put this is your ~/.zshrc to make it permanent.)


  • Related Question

    unix - shell dotfiles and *rcs: what's a sane setup?
  • kch

    A bash user will eventually end up with .bashrc, .bash_profile, .profile, and maybe some more.

    Now, each file gets loaded unders particular situations, and it all leads to confusion and frustration. I don't care about what shell is a login shell and neither should you.

    I just want to make sure the same thing is loaded for every shell thing that happens.

    So, what's the sane way to set them up?

    I'd wager non-bash-specific things go into .profile, and some file sources the others, etc. What exactly would in put in each to achieve an identical environment for every shell?

    Note: I'm not asking what you particularly enjoy putting in your rc files, like aliases and functions and so on. Just how you lay them out so as not to have things randomly spliced amongst them.


  • Related Answers
  • Jonathan Leffler

    I just want to make sure the same thing is loaded for every shell thing that happens.

    If you really want that, put everything in ~/.profile and add a source ~/.profile at the end of your ~/.bashrc. If this is desirable is a different question. To source ~/.profile in ~/.bashrc is a very common setup anyway.

    +------------+-----------------+--------------------+
    |            | login shells    | interactive shells |
    +------------+-----------------+--------------------|
    | all        | /etc/profile    |                    |
    | bourneish  | ----------------+--------------------|
    | shells     | ~/.profile      |                    |
    +------------+-----------------+--------------------|
    | just       | ~/.bash-profile | /etc/bash.bashrc   |
    |            | -------------------------------------|
    | bash       | ~/.bash-login   | ~/.bashrc          |
    +------------+-----------------+--------------------+
    

    C shell and shells derived use a different set of files (.login, .cshrc, ..).

    What exactly would in put in each to achieve an identical environment for every shell?

    If you mean environment in the sense of environment variables, just set all of them in ~/.profile and you are OK. If you mean environment in a broader sense, it depends.

    The issue here is that it is not desirable to have the very same environment for interactive and login shells. An example is aliases: Maybe you want aliases in your interactive shell, but very likely they will make your scripts do weird things. So you don't want your aliases in non-interactive shells => put them in ~./bashrc.