gpt4 book ai didi

bash - 如何在 bash 中打印变量的值(可以是 "-n"或 "-e")?

转载 作者:行者123 更新时间:2023-11-29 09:22:27 35 4
gpt4 key购买 nike

如何从 bash 打印“-n”、“-e”或“-neeenen”(末尾没有换行符,不调用外部程序)?

Q="-n"

echo -n "$Q" #失败

echo -- "$Q" #失败

cat <<< "$Q" # 失败,同时启动外部程序

printf -- '%s' "$Q" # 成功,但启动外部程序

最佳答案

在 bash 中,printf 是内置的,因此不会调用外部程序。

$ help printf 
printf: printf [-v var] format [arguments]

printf formats and prints ARGUMENTS under control of the FORMAT. FORMAT is a character string which contains three types of objects: plain characters, which are simply copied to standard output, character escape sequences which are converted and copied to the standard output, and format specifications, each of which causes printing of the next successive argument. In addition to the standard printf(1) formats, %b means to expand backslash escape sequences in the corresponding argument, and %q means to quote the argument in a way that can be reused as shell input. If the -v option is supplied, the output is placed into the value of the shell variable VAR rather than being sent to the standard output.

关于bash - 如何在 bash 中打印变量的值(可以是 "-n"或 "-e")?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3070246/

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