最近文章阅读排行榜****
********
【爬虫基础】第一部分 网络通讯-Socket套接字 P2/3-CSDN博客
【Linux运维】非root用户的单向免密登录_linux 单向免密-CSDN博客
【Linux运维】服务器信息查询_linux 查询机器所有信息脚本-CSDN博客
1.背景 
echo
是一个常用的输出命令,它可以将指定的字符串或变量输出到终端。
需求:在跑批脚本结束,通常需要打印当前时间到文件中,方便查看。
本文以Linux 终端输出字符串 和 时间为例进行实验,虽然简单,整理一下。
2. 实验
2.1 实验:date
date
说明:linux 输入date,默认输出当前的日期+星期+时间
date +"格式" 或 date "+格式"
说明:按制定格式输出需要的日期及时间
sql
[oracle@neptune ~]$ date
Fri Feb 21 14:16:11 CST 2025
[oracle@neptune ~]$ date "+%Y-%m-%d %H:%M:%S"
2025-02-21 14:16:19
[oracle@neptune ~]$ date +"%Y-%m-%d %H:%M:%S"
2025-02-21 14:16:29
[oracle@neptune ~]$ date "+%Y-%m-%d"
2025-02-21
[oracle@neptune ~]$ date "+%F"
2025-02-21
[oracle@neptune ~]$ date "+%F %T %A"
2025-02-21 14:21:23 Friday
结果截图:

2.2 实验 echo 时间
(1)简单输出
sql
输出年月日:echo $(date +%F)
输出时分:echo $(date +%R)
输出时分秒:echo $(date +%T)
输出星期:echo $(date +%A)
结果截图:

(2)输出多个格式
需要用双引号包起来,否则报错.
sql
[oracle@neptune ~]$ echo $(date "+%F %T %A")
2025-02-21 14:26:52 Friday
[oracle@neptune ~]$
[oracle@neptune ~]$ echo $(date +%F %T %A)
date: extra operand '%T'
Try 'date --help' for more information.
[oracle@neptune ~]$
结果截图:
(3)组合格式输出
sql
[oracle@neptune ~]$ echo $(date +"%Y-%m-%d %H:%M:%S")
2025-02-21 14:31:07
[oracle@neptune ~]$ echo $(date +"%Y-%m-%d %H:%M:%S %A")
2025-02-21 14:31:16 Friday
[oracle@neptune ~]$ echo $(date +"%F %T %A")
2025-02-21 14:31:28 Friday
[oracle@neptune ~]$ echo `date`
Fri Feb 21 14:36:38 CST 2025
[oracle@neptune ~]$
[oracle@neptune ~]$ echo `date +%F`
2025-02-21
[oracle@neptune ~]$ echo `date +%F %T`
date: extra operand '%T'
Try 'date --help' for more information.
[oracle@neptune ~]$ echo `date "+%F %T"`
2025-02-21 14:37:44
[oracle@neptune ~]$
结果截图:

2.3 实验 echo 字符串
echo "Hello, World" 或 echo Hello, World
说明:linux 输出字符串,直接跟在echo之后即可,可加双引号 或不加。
echo "Hello," "ZhangSan"
说明:linux 输出字符串拼接。
first_name="John"
last_name="Doe"
echo "Full name: $first_name $last_name"
拼接多个变量和字符串,只需将它们按顺序放在
echo
命令中。echo "Hello, $name"
说明:linux 输出字符串,同时字符串中有变量。
注意:
单引号:其中的内容会被原样输出,不会解析变量和特殊字符。
双引号:其中的内容会被解析,变量和特殊字符会被替换为相应的值。
[oracle@neptune ~]$ name=1
[oracle@neptune ~]$ echo "The name is $name ian"
The name is 1 ian
[oracle@neptune ~]$ echo 'The name is $name ian'
The name is $name ian
[oracle@neptune ~]$
(1)字符串无变量
bash
[oracle@neptune ~]$ echo ss
ss
[oracle@neptune ~]$ echo "ss"
ss
[oracle@neptune ~]$ echo "ss" "你好"
ss 你好
[oracle@neptune ~]$ echo "ss""你好"
ss你好
[oracle@neptune ~]$ echo "ss""你好"",欢迎"
ss你好,欢迎
[oracle@neptune ~]$ echo ss 你好,欢迎
ss 你好,欢迎
[oracle@neptune ~]$ echo ss 你好,欢迎!
ss 你好,欢迎!
结果截图:

(2)字符串有变量
bash
[oracle@neptune ~]$ name=Zhangsan
[oracle@neptune ~]$ echo name
name
[oracle@neptune ~]$ echo `name`
-bash: name: command not found
[oracle@neptune ~]$ echo $name
Zhangsan
[oracle@neptune ~]$ echo "欢迎:"$name
欢迎:Zhangsan
[oracle@neptune ~]$ echo "欢迎:$name"
欢迎:Zhangsan
[oracle@neptune ~]$
结果截图:

(3)拼接的几个变量
echo 后用 **$**变量 取,多个变量可以用双引号包起来 。
当变量名与后续字符容易混淆时,可以使用**${}**来明确变量的边界。
bash
[oracle@neptune ~]$ name="Alice"
[oracle@neptune ~]$ greeting="Hello, "
[oracle@neptune ~]$ echo $greeting$name
Hello, Alice
[oracle@neptune ~]$ echo "$greeting$name"
Hello, Alice
[oracle@neptune ~]$ echo "$greeting $name"
Hello, Alice
[oracle@neptune ~]$ echo "$greeting 欢迎 $name"
Hello, 欢迎 Alice
[oracle@neptune ~]$ echo $greeting 欢迎 $name
Hello, 欢迎 Alice
[oracle@neptune ~]$
结果截图:

当变量名与后续字符容易混淆时,可以使用${}来明确变量的边界。
bash
[oracle@neptune ~]$ name="Alice"
[oracle@neptune ~]$ echo "The name is ${name}ian"
The name is Aliceian
[oracle@neptune ~]$ echo "The name is $name ian"
The name is Alice ian
[oracle@neptune ~]$ echo "The name is $nameian"
The name is
[oracle@neptune ~]$
结果截图:

2.4 实验 echo 字符串+时间
使用 echo 字符串 $(date "格式") ,可以用双引号 包起来
bash
[oracle@neptune ~]$ echo "跑批结束!"
跑批结束!
[oracle@neptune ~]$ echo "跑批结束!结束时间为:" `date`
跑批结束!结束时间为: Fri Feb 21 14:58:00 CST 2025
[oracle@neptune ~]$
[oracle@neptune ~]$ echo "跑批结束!结束时间为:" $(date "+%F %T %A")
跑批结束!结束时间为: 2025-02-21 15:04:12 Friday
[oracle@neptune ~]$ echo "跑批结束!结束时间为: $(date "+%F %T %A")"
跑批结束!结束时间为: 2025-02-21 15:05:08 Friday
[oracle@neptune ~]$
结果截图:


3. 在脚本中 echo
bash
#!/bin/bash
current_time=$(date +"%Y-%m-%d %H:%M:%S")
echo "当前系统时间是:$current_time"
4.相关知识点
4.1 echo -n
表示:不换行。默认是换行的
4.2 echo -e
表示:前面有反斜线的字符将作为转义字符 ,但是需要要有单引号 或者 双引号包含。
bash
[oracle@neptune ~]$ echo \n\n 1234
nn 1234
[oracle@neptune ~]$ echo -e \n\n 1234
nn 1234
[oracle@neptune ~]$ echo -e '\n\n 1234'
1234
[oracle@neptune ~]$ echo -e '\n\n' 1234
1234
[oracle@neptune ~]$
bash
[oracle@neptune ~]$ echo -e "\\\\" 1234
\ 1234
[oracle@neptune ~]$ echo -e "\\ \\" 1234
\ \ 1234
[oracle@neptune ~]$
结果截图:

4.3设置文本颜色的转义符
\e[<格式代码>m 或者 \033[<格式代码>m
说明:基本上是夹在 "\e["(转义开方括号)和 "m" 之间数字值。指定一个以上的格式代码(数字),则用分号将他们分开。
\e[A;B;Cm 或者 \033[A;B;Cm
说明:A 为格式控制,编号0~9;
B 为字体颜色,编号30~37;
C 为背景色,编号40~47。
可通过 \e[0m 或 \033[0m 关闭颜色输出; \e[A;B;Cm 内容 \e[0m
\e 即 \033, 结尾是 \e[0m 清理格式
1) 格式代码:A 0-9

2) 字体颜色 B 30-27 | 背景颜色 C 40-47

echo -e '\e[4;31;43m 我是红色字体 黄色背景 下划线!\e[0m'
bash
[oracle@neptune ~]$ echo -e '\e[4;31;43m 我是红色字体 黄色背景 下划线!\e[0m'
我是红色字体 黄色背景 下划线!
[oracle@neptune ~]$ echo 1
1
截图:

如果在结尾不关格式,则为以下结果:

本节实验结束:ok
项目管理--相关知识
项目管理-项目绩效域1/2_八大绩效域和十大管理有什么联系-CSDN博客
项目管理-计算题公式【复习】_项目管理进度计算题公式:乐观-CSDN博客
项目管理-相关知识(组织通用治理、组织通用管理、法律法规与标准规范)-CSDN博客
Oracle其他文档,希望互相学习,共同进步
Oracle-找回误删的表数据(LogMiner 挖掘日志)_oracle日志挖掘恢复数据-CSDN博客
oracle 跟踪文件--审计日志_oracle审计日志-CSDN博客
ORA-12899报错,遇到数据表某字段长度奇怪现象:"Oracle字符型,长度50"但length查却没有50_varchar(50) oracle 超出截断-CSDN博客