undefined

1. shell 脚本中如何在字符串中传递变量

分情况

  1. 字符串本身用单引号
    这个 ${num} 添加单引号

    1
    2
    3
    4
    num=100
    echo "num:${num}"
    sentence='hello, I am frank. '${num}''
    echo "sentence:${sentence}"
  2. 字符串本身用双引号
    可以直接使用 ${num} 的形式

    1
    2
    3
    4
    num=100
    echo "num:${num}"
    sentence="hello, I am frank.${num}"
    echo "sentence:${sentence}"

2. shell 脚本中 比较 类运算符的表示方法

> -gt
< -lt
>= -ge
<= -le
!= -ne

3. 日期格式化

1
2
3
4
5
6
7
8
9
curday=`date +%Y-%m-%d`
echo $curday

curday2=`date '+%Y-%m-%d %H:%M:%S'`
echo $curday2

# 输出
2022-06-23
2022-06-23 09:54:37

4. 字符串拼接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
key="hello"
val="world"

str1=$key$val # 中间不能有空格
str2="$key $val" # 如果被双引号包围,那么中间可以有空格
str3=$key"+++"$val # 中间可以出现别的字符串
str4="$key++$val"
str5="${key}Script+++ ${val}index" # 给变量加上花括号做区分

# 输出
helloworld
hello world
hello+++world
hello++world
helloScript+++ worldindex