博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
shell编程之echo printf 命令
阅读量:4568 次
发布时间:2019-06-08

本文共 962 字,大约阅读时间需要 3 分钟。

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'

$

转载于:https://www.cnblogs.com/techdreaming/p/5218271.html

你可能感兴趣的文章
Redis过期命令
查看>>
Redis键的序列化和反序列化
查看>>
redis键的迁移操作
查看>>
redis字符串类型的基本命令
查看>>
redis哈希表数据类型键的设置
查看>>
redis字符串类型键的二进制操作
查看>>
redis哈希表数据类型键的查询和删除命令
查看>>
redis列表数据类型---list
查看>>
redis集合数据类型---SET
查看>>
php 字符串函数
查看>>
PHP数组函数
查看>>
PHP Math函数
查看>>
PHP Math常量
查看>>
idea使用git上传项目到coding
查看>>
SpringMVC+mybatis配置多数据源
查看>>
mybatis中条件查询大于等于和小于等于写法
查看>>
mybatis无法给带有下划线属性赋值问题
查看>>
java.lang.NoSuchMethodException: com.sun.tools.javac.util.List.<init>()
查看>>
Could not set property of class with value There is no setter for property named
查看>>
Could not find result map com.youotech.tl_cons_credit_rating.entity.Result
查看>>