变量a
是一个带空格的字符串,现在用"hdpusr400"替换变量a
中的"hduser302":
字符串变量的替换方式
定义变量a1 2
| [liusiyi@localhost ~]$ echo $a hduser302 hduser302 /apps/hduser302/student/
|
用变量替换${a//}做字符替换
用变量替换${a//}做字符替换1 2 3 4 5 6 7
| [liusiyi@localhost ~]$ echo ${a/hduser302/hdpusr400} hdpusr400 hduser302 /apps/hduser302/student/
[liusiyi@localhost ~]$ echo ${a//hduser302/hdpusr400} hdpusr400 hdpusr400 /apps/hdpusr400/student/
|
用 sed 做字符串替换
用sed做字符串替换1 2 3 4 5 6 7
| [liusiyi@localhost ~]$ echo $a | sed 's/hduser302/hdpusr400/' hdpusr400 hduser302 /apps/hduser302/student/
[liusiyi@localhost ~]$ echo $a | sed 's/hduser302/hdpusr400/' hdpusr400 hdpusr400 /apps/hdpusr400/student/
|
用 awk 做字符串替换
用awk做字符串替换1 2 3 4 5 6 7
| [liusiyi@localhost ~]$ echo $a | awk '{gsub(/hduser302/,"hdpusr400",$3);print $0}' hdpusr400 hduser302 /apps/hduser302/student/
[liusiyi@localhost ~]$ echo $a | awk '{gsub(/hduser302/,"hdpusr400");print $0}' hdpusr400 hdpusr400 /apps/hdpusr400/student/
|
数组所有元中的字符串替换
数组所有元替换1 2
| Arr=($(ls -d /run/media/$USER/*/*)) Brr=(${Arr[*]//"/run/media/$USER"/"$HOME"})
|
Arr
数组元中,所有元包括绝对路径/run/media/$USER
,
而Brr
数组将其全部替换为$HOME
.