如何在变量周围设置引号,以便程序将它们视为引号

uxs*_*-bw 6 bash string shell-script quoting

我试图在一个变量周围加上引号,就像我在终端中输入它以使这个脚本工作一样。它显示“变量周围的引号”,但仍然没有将其视为引号,据我所知,因为它不起作用。

    hs='#'

echo ${hs}${hex}
echo  "\" #${hex} \""
echo  "\"#${hex}\""
#one way trying
hsetroot -solid "\"${hs}${hex}\""
#another way of trying
hsetroot -solid "\""${hs}${hex}"\""
Run Code Online (Sandbox Code Playgroud)

无论哪种方式,我都将“ qoutes ”放在变量周围,正如您所看到的,它们都在两边都有引号,但它仍然不起作用。但是,如果我在终端中输入“#31064d”并将其放在需要的位置,则它可以工作。输出:

#31064d
" #31064d "
"#31064d"
Bad color ("#31064d") 
Bad color ("#31064d")
Run Code Online (Sandbox Code Playgroud)

这是一个很好的颜色代码

userx@bw2-crunchbang:~/testscript$ hsetroot -solid "#31064d"
Run Code Online (Sandbox Code Playgroud)

我如何在脚本中格式化它,以便需要婴儿车用引号引起来的程序在引号中看到它?

我也试过这样写。

echo "\""#${hex}"\""
hsetroot -solid "\"#${hex}\""
Run Code Online (Sandbox Code Playgroud)

输出:

Bad color ("#12eba5")
Run Code Online (Sandbox Code Playgroud)

你看到“引号”仍然添加在数字和字母周围,就像在终端中输入一样,但我不知道,不工作。

我也试过 ' ' 和标记,不行。

gle*_*man 5

取出文字引号:它们导致错误。

shell 使用引号来转义某些字符(在这种情况下是散列,否则会被视为注释的开头),等等。当 shell 调用另一个命令并将带引号的参数传递给它时,它不包括文字引号,它们会被剥离。

你只需要这个:

hsetroot -solid "${hs}${hex}"
Run Code Online (Sandbox Code Playgroud)