Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable a zsh special variable (bang tilde !~)?

Tags:

zsh

My zsh is replacing !~ with a command I previously ran. This is a problem because when I run awk '$1 !~ /abc/, it replaces the !~ with the command.

Any idea on how to disable this? It's possible it's not zsh's fault, but after googling for an hour and not finding anything I decided it was the most likely candidate.

UPDATE:

This only happens when !~ occurs on a newline:

echo !~
# ~/bin/test_translate.rb

echo foo | awk '
$1 !~ /abc/'

awk: cmd. line:2:     $1 ~/bin/test_translate.rb /abc/
awk: cmd. line:2:                            ^ syntax error
awk: cmd. line:3:     $1 ~/bin/test_translate.rb /abc/
awk: cmd. line:3:                                     ^ unexpected newline or end of string

UPDATE 2:

I've narrowed it down to this line in my .zshrc:

source $ZSH/oh-my-zsh.sh

I would like to find out what option is making zsh replace !~ plus spacebar into the last path I accessed, but I don't want to stop using oh-my-zsh. I haven't manually modified $ZSH/oh-my-zsh.sh.

like image 948
nachocab Avatar asked Dec 07 '22 06:12

nachocab


1 Answers

In zsh (and some other shells) ! triggers the history expansion (see man zshexpn section "HISTORY EXPANSION"). In the case of !~ zsh will look for the most recent command starting with ~.

If you do not use history expansion in any way, you can just disable it by adding this to your .zshrc

setopt nobanghist

Usually history expansion is not done inside single quoted strings, so it should not affect your awk command in any way:

$ echo !~
~/docs
$ echo foo | awk '
$1 !~ /abc/'            
foo

On the other hand, inside double quotes history expansion is done and you get the mentioned error message:

$ echo !~
~/docs
$ echo foo | awk "
$1 !~ /abc/"   
echo foo | awk "
$1 ~/docs /abc/"
awk: cmd. line:2:  ~/docs /abc/
awk: cmd. line:2:  ^ syntax error
awk: cmd. line:3:  ~/docs /abc/
awk: cmd. line:3:              ^ unexpected newline or end of string

So, if the problem can be traced to source $ZSH/oh-my-zsh.sh, I'd guess that Oh, my ZSH does something to break the way zsh handles quoting.

like image 130
Adaephon Avatar answered Mar 20 '23 01:03

Adaephon