0

Saw the use of !$ on this site, so I made a short bash script "bang_dollar.sh" as below:

#!/bin/bash
echo -e '\n'

touch touche.txt vi !$

This was a copy verbatim of what someone posted as a solution on this site (to a different question).

I ran

$ touch touche.txt                           
$ vi !$

from the command line, and vi does in fact open touche.txt for editing.

My bang_dollar.sh script however, creates a file named "touche.txt", and vi opens,
but "!$" is literally interpreted as !$, so vi opens a file for editing named "!$".

Is it possible to use "!$" in a bash script with some sort of masking? And shouldn't a file named "!$" be illegal on BASH 5.1.4?

Romeo Ninov
  • 17,484
  • "shouldn't a file named "!$" be illegal" - No. (Or well, if you asked me, it should, not just in the shell but in the OS. But it isn't.) – ilkkachu Aug 30 '23 at 20:15
  • 1
    In a script, I'd rather use a variable than rely on history expansion, file=touche.txt; vi "$file". But you do you. – ilkkachu Aug 30 '23 at 20:16

0 Answers0