Reputation: 65
I have a variable that has a hash value assigned to it. For example :
hash=$1$qGqTE/jV$syM.7qpaKlCTsBXOYu2op/
now when I do echo $hash
in bash it returns:
/jV.7qpaKlCTsBXOYu2op/
How do I have hash value not escape any characters? or have echo $hash
return the entire string $1$qGqTE/jV$syM.7qpaKlCTsBXOYu2op/
?
Any help is highly appreciated.
Upvotes: 2
Views: 900
Reputation: 113814
The definition needs single quotes:
$ hash='$1$qGqTE/jV$syM.7qpaKlCTsBXOYu2op/'
$ echo "$hash"
$1$qGqTE/jV$syM.7qpaKlCTsBXOYu2op/
Without the single quotes, the shell performs variable substitution and the result depends on the value returned by $1
, $qGqTE
and $syM
when the definition statement is executed.
I also added double-quotes to the echo
statement. This stops the shell from performing word splitting and pathname expansion. While it may be unlikely that a hash value would be affected by these, it is safer to use the double quotes. As an example of the potential problem:
$ hash='/bin/le*'
$ echo $hash
/bin/less /bin/lessecho /bin/lessfile /bin/lesskey /bin/lesspipe
$ echo "$hash"
/bin/le*
As you can see, in this case, echo $hash
performs pathname expansion and returns a list of files. echo "$hash"
, however, works as desired. To avoid surprises such as this, it is best to put references to shell variables in double quotes.
Upvotes: 3