0

When I am assigning .* to a variable , it is assigning all hidden files.

[root@s1 ~]# a=".*"
[root@s1 ~]# echo $a
. .. .bash_history .bash_logout .bash_profile .bashrc .cache .config .cshrc .history .lesshst .mozilla .pki .rnd .ssh .tcshrc .viminfo .virsh .xauth6SHzeY .xauthhAVYfm .xauthI6Cte3 .xauthk7ea35 .xauthlXtiZ9 .Xauthority .xauthQm7mJ8 .xauthTpWbxP .xauthY9KsdC

I expect below output :

.*

How to escape it, thanks.

I tried below and it gives output

 [root@s1 ~]# a='".*"'
    [root@s1 ~]# echo $a
    ".*"

".*"

but not

.*

Bharat
  • 814
  • I don't get why people downvote this. These problems are very common when you start using bash, everyone here had these. And research is difficult with terms like that and when you don't know the exact problem. – pLumo Aug 10 '18 at 13:40
  • thanks you all, please let me know if it is worth accepting and leaving the question active, or better to delete it. – Bharat Aug 10 '18 at 13:50
  • marking it as duplicate should be fine, you can leave it open. Like that people can more search terms to find the answer. – pLumo Aug 10 '18 at 14:38

2 Answers2

6

Since you need to call:

echo ".*"

in order to not get this expanded, you of course also need to call:

a=".*"
echo "$a"
schily
  • 19,173
6

You're doing nothing wrong in your assignment of the string .* to the variable, but since you use the variable unquoted with echo, the shell will perform (word splitting and) filename generation (globbing) on its value before calling echo.

To prevent this, double quote the variable expansion:

echo "$a"

Related:

Kusalananda
  • 333,661