如何将 .* 作为值分配给 bash 中的变量

Bha*_*rat 0 bash variable

当我将 .* 分配给变量时,它正在分配所有隐藏文件。

[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
Run Code Online (Sandbox Code Playgroud)

我期望以下输出:

.*

怎么解决,谢谢。

我在下面试过,它给出了输出

 [root@s1 ~]# a='".*"'
    [root@s1 ~]# echo $a
    ".*"
Run Code Online (Sandbox Code Playgroud)

“.*”

但不是

.*

sch*_*ily 6

由于您需要致电:

echo ".*"
Run Code Online (Sandbox Code Playgroud)

为了不让这个扩展,你当然还需要调用:

a=".*"
echo "$a"
Run Code Online (Sandbox Code Playgroud)


Kus*_*nda 6

您在将字符串分配.*给变量时没有做错任何事情,但是由于您使用了不带引号的变量echo,shell 将在调用echo.

为了防止这种情况,双引号变量扩展:

echo "$a"
Run Code Online (Sandbox Code Playgroud)

有关的: