shell编程之echo printf 命令
2016-02-25 20:41
531 查看
shell中 echo 和printf 都能用作输出,printf可以算是echo的增强版
显示转义字符
echo \""abcdef\""
>>> "abcdef"
显示变量
age=23
echo "my age is $age"
>>>my name is 23
在使用的过程中,为了避免引起歧义,多使用${age}
显示换行
echo “ok\n“
echo "my name is liming"
>>>ok
>>>my name is liming
显示结果重定向
e cho "my name is limng "> file1
printf 格式化输出语句,但是后面必须加上回车换行符,不像echo 不用加
printf “acbdef\n"
>>>abcdef
>>>
# format-string为双引号
$ printf "%d %s\n" 1 "abc"
1 abc
# 单引号与双引号效果一样
$ printf '%d %s\n' 1 "abc"
1 abc
# 没有引号也可以输出
$ printf %s abcdef
abcdef
# 格式只指定了一个参数,但多出的参数仍然会按照该格式输出,format-string 被重用
$ printf %s abc def
abcdef
$ printf "%s\n" abc def
abc
def
$ printf "%s %s %s\n" a b c d e f g h i j
a b c
d e f
g h i
j# 如果没有 arguments,那么 %s 用NULL代替,%d 用 0 代替
$ printf "%s and %d \n"
and 0
# 如果以 %d 的格式来显示字符串,那么会有警告,提示无效的数字,此时默认置为 0
$ printf "The first program always prints'%s,%d\n'" Hello Shell-bash: printf: Shell: invalid numbe
The first program always prints 'Hello,0'
$
显示转义字符
echo \""abcdef\""
>>> "abcdef"
显示变量
age=23
echo "my age is $age"
>>>my name is 23
在使用的过程中,为了避免引起歧义,多使用${age}
显示换行
echo “ok\n“
echo "my name is liming"
>>>ok
>>>my name is liming
显示结果重定向
e cho "my name is limng "> file1
printf 格式化输出语句,但是后面必须加上回车换行符,不像echo 不用加
printf “acbdef\n"
>>>abcdef
>>>
# format-string为双引号
$ printf "%d %s\n" 1 "abc"
1 abc
# 单引号与双引号效果一样
$ printf '%d %s\n' 1 "abc"
1 abc
# 没有引号也可以输出
$ printf %s abcdef
abcdef
# 格式只指定了一个参数,但多出的参数仍然会按照该格式输出,format-string 被重用
$ printf %s abc def
abcdef
$ printf "%s\n" abc def
abc
def
$ printf "%s %s %s\n" a b c d e f g h i j
a b c
d e f
g h i
j# 如果没有 arguments,那么 %s 用NULL代替,%d 用 0 代替
$ printf "%s and %d \n"
and 0
# 如果以 %d 的格式来显示字符串,那么会有警告,提示无效的数字,此时默认置为 0
$ printf "The first program always prints'%s,%d\n'" Hello Shell-bash: printf: Shell: invalid numbe
The first program always prints 'Hello,0'
$
相关文章推荐
- Java如何操作Android的adb shell 之 我自己在程序中的使用方法
- Java如何操作Android的adb shell
- 哪些晦涩难懂的shell命令都是什么的缩写?
- adb shell dumpsys命令
- linux系统shell变量入门
- 【sehll学习】linux运维一个简单shell脚本监控系统内存
- shell之磁盘容量检查
- shell之磁盘容量检查
- 自认为比较经典的shell程序
- shell脚本实例-交叉证认
- shell获取 linux 系统的位数
- powershell 工作流作业
- linux中java调用shell脚本
- ansible模块command、shell、raw、script
- xshell里面使用黑色背景时蓝色字体看不清楚的解决方法
- shell printf命令:格式化输出语句
- shell 中数学计算总结
- 如何在shell脚本中判断文件或者文件夹是否存在?
- 如何在shell脚本中判断文件或者文件夹是否存在?
- 如何在shell脚本中判断文件或者文件夹是否存在?