如何在bash中打印变量的值(可以是"-n"或"-e")? [英] How to print value of a variable (which can be "-n" or "-e") in bash?

查看:62
本文介绍了如何在bash中打印变量的值(可以是"-n"或"-e")?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从bash打印"-n",-e"或"-neeenen"(末尾没有换行符,而无需调用外部程序)?

How to print "-n", "-e" or "-neeenen" from bash (without a newline at the end, without invoking of external programs)?

Q =-n"

echo -n"$ Q" #失败

echo-"$ Q" #失败

cat<<<"$ Q" #失败,还会启动外部程序

cat <<< "$Q" # fail, also starts external program

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

推荐答案

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

In bash, printf is a builtin, so no external program is invoked.

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

printf在FORMAT的控制下格式化并打印ARGUMENTS.格式是包含三种类型的对象的字符串:普通字符,只需将其复制到标准输出中,字符逃脱被转换并复制到标准输出的序列,以及格式规范,每一个都会导致下一个的打印连续争论.除了标准的printf(1)格式,%b表示至在相应的参数中扩展反斜杠转义序列,然后%q表示以一种可以作为shell输入重用的方式引用该参数.如果提供了-v选项,则将输出放置在shell变量VAR而不是发送到标准输出.

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中打印变量的值(可以是"-n"或"-e")?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆