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

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

问题描述

如何打印-n,-E或-neeenen从庆典(结尾没有一个换行符,无需外部程序调用的)?

Q = - N

回声-n$ Q#失败

回声 - $ Q#失败

猫<<< $ Q#失败,也将启动外部程序

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


解决方案

在bash中,的printf 是一个内置的,因此无需外部程序被调用。


  $帮助的printf
的printf:printf的[-v VAR]格式[参数]


  
  

下的格式控制printf的格式和打印参数。
  格式
      是包含三种类型的对象的字符串:纯
      字符,被简单地复制到标准输出,字符
  逃逸
      被转换和复制到标准输出序列,和
      格式规范,其中每一个使下一个印刷
  连续
      论据。除了标准printf(1)格式,%B装置
  至
      扩大反斜线逃避相应的参数序列和%Q
      装置引用的论点在于,可重复使用作为外壳输入的方法。
      如果-v选项,输出放置到的价值
      外壳变量VAR而不是发送到标准输出。


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" # fail

echo -- "$Q" # fail

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

printf -- '%s' "$Q" # success, but starts external program

解决方案

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

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

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