gpt4 book ai didi

linux - Zenity——带有字符串变量的文本

转载 作者:太空宇宙 更新时间:2023-11-04 10:42:12 24 4
gpt4 key购买 nike

我想知道如何将包含空格的字符串传递给 Zenity 作为文本参数,因为我当前的方法正在截断/无法评估第一个空格后的所有文本。

这是一个显示问题的 MVP:

脚本

#!/bin/bash

a="test test test"
test_func() {
echo "$@"
$(zenity --info --text "test test")
$(zenity --info --text "$@")
}

test_func ${a}

输出

$> test test test
$> (zenity info window with test test as text)
$> (zenity info window with test) *** should contain "test test test"

最佳答案

使用 "$*" 而不是 "$@"

"$@" 手册(强调我的):

Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" ... If the double-quoted expan sion occurs within a word, the expansion of the first parameter is joined with the beginning part of the orig‐ inal word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).

对于 "$*":

Expands to the positional parameters, starting from one. When the expansion is not within double quotes, each positional parameter expands to a separate word. In contexts where it is performed, those words are subject to further word splitting and pathname expansion. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent to "$1c$2c...", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.

或者,您可以引用 ${a}a 作为单个参数传递,而不是按空格拆分。

关于linux - Zenity——带有字符串变量的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34598050/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com