-2

I have a command line that takes a password . The password starts with characters '!1'. E.g. '!1x9y377s' . If I execute a command with that password, then history expansion occurs and the first two letters '!1' are replaced with the 1st history entry. E.g.

ll
...list entries
echo !1x9y377s
llx9y377s

but what I wanted was:

ll
...list entries
echo !1x9y377s
!1x9y377s

Research

I soon realized that this was history expansion occurring prior to bash parsing. I searched for: How can I stop history expansion from occurring on passwords passed in the command line? Which resulted in things like this :

  1. superuser
  2. stackover
  3. berkley tcsh docs It states: "The `!' may be preceded by a `\' to prevent its special meaning"

I tried executing:

ll
...list entries
echo '!1x9y377s'
!1x9y377s

and

ll
...list entries
echo \!1x9y377s
!1x9y377s

both of which worked.

1 Answers1

3

Their are two ways to approach this. According to the berkley docs: "The ! may be preceded by a \ to prevent its special meaning"

But, if you don't want to go escaping, use a single quote around the password. E.g.

ll
...list entries
echo '!1x9y377s'
!1x9y377s

According to the Bash Manual: 3.1.2.2 Single Quotes

Enclosing characters in single quotes (') preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.