0

How do we change bash character ! for special history command to other direct single key character such as = ?

1 Answer 1

1

This can be done using the histchars variable:

Up to three characters which control history expansion, quick substitution, and tokenization (see History Interaction). The first character is the history expansion character, that is, the character which signifies the start of a history expansion, normally '!'. The second character is the character which signifies 'quick substitution' when seen as the first character on a line, normally '^'. The optional third character is the character which indicates that the remainder of the line is a comment when found as the first character of a word, usually '#'. The history comment character causes history substitution to be skipped for the remaining words on the line. It does not necessarily cause the shell parser to treat the rest of the line as a comment.

So:

$ echo foo
foo
$ histchars='=^'
$ =echo
echo foo
foo
$ 

But: there are occasional assumptions in the code that ! is the history expansion character (mostly for setting exceptions, like in globbing). Setting it to = will break a lot of things, like setting variables:

$ a=e
aecho foo
bash: aecho: command not found
$ a'='e
bash: a=e: command not found
$ a\=e
bash: a=e: command not found
$ printf -v a e
$ echo $a
e

You must log in to answer this question.