当前位置:网站首页>Shell script - string
Shell script - string
2022-07-01 08:47:00 【Little snail's way】
character string (String) It is a combination of a series of characters . The string is Shell One of the most commonly used data types in programming ( Besides numbers and strings , There are no other types ).
Strings can be made of single quotes ' ' Surround , You can also use double quotes " " Surround , You can also use no quotes .
String example
str1=jack
str2="rose"
str3='100'
The difference between the three forms
- By single quotes
' 'Surrounded string :
- Any character will be output as it is , Using variables in it is not valid .
- Single quotation marks... Cannot appear in a string , Even if you escape single quotation marks .
- By double quotation marks
" "Surrounded string :
- If it contains a variable , Then the variable will be resolved ( Get the value of the variable ), Instead of outputting it as it is .
- Double quotation marks can appear in the string , As long as it is escaped .
- A string that is not surrounded by quotation marks
- Variables in strings that are not surrounded by quotation marks are also parsed , This point and double quotation marks
" "The surrounding string is the same . - No spaces in the string , Otherwise, the string after the space will be parsed as other variables or commands .
Script
#!/bin/bash
name=jack
str1= My name is $name
str2=" There's a personal \" name \" It's called $name"
str3=' My friend's name is $name'
echo $str1
echo $str2
echo $str3
Output :
My name is jack
There's a personal " name " It's called jack
My friend's name is $name
str1 It contains $name, It is resolved to a variable name References to .
str2 Contains quotation marks , But it was escaped ( By backslash \ The beginning indicates the escape character ).str2 It also includes $name, It is also resolved as a variable name References to .
str3 It also includes $name, But only as ordinary characters , Does not resolve to a variable name References to .
Get string length
The specific method is as follows ,${#string_name},string_name Represents the string name .
The code is as follows :
#!/bin/bash
name=jack
echo ${#name}
Output :
4
边栏推荐
- 你了解数据是如何存储的吗?(C整型和浮点型两类)
- Shell脚本-数组定义以及获取数组元素
- Introduction to 18mnmo4-5 steel plate executive standard and delivery status of 18mnmo4-5 steel plate, European standard steel plate 18mnmo4-5 fixed rolling
- What is the material of 15CrMoR, mechanical properties and chemical analysis of 15CrMoR
- factory type_id::create过程解析
- Insert mathematical formula in MD document and mathematical formula in typora
- Embedded Engineer Interview frequently asked questions
- 个人装修笔记
- Only in China! Alicloud container service enters the Forrester leader quadrant
- C语言指针的进阶(上篇)
猜你喜欢

Matlab tips (23) matrix analysis -- simulated annealing

Centos7 shell脚本一键安装jdk、mongo、kafka、ftp、postgresql、postgis、pgrouting

C语言指针的进阶(上篇)
![[MFC development (17)] advanced list control list control](/img/e8/24c52cb51defc6c96b43c2ef3232ff.png)
[MFC development (17)] advanced list control list control

Memory size end

Foundation: 2 The essence of image

如何做好固定资产管理?易点易动提供智能化方案

【无标题】

《单片机原理及应用》—定时器、串行通信和中断系统

What is the material of 16MnDR, the minimum service temperature of 16MnDR, and the chemical composition of 16MnDR
随机推荐
【MFC开发(16)】树形控件Tree Control
SPL Introduction (I)
Only in China! Alicloud container service enters the Forrester leader quadrant
1.jetson与摄像头的对接
《单片机原理与应用》——并行IO口原理
大型工厂设备管理痛点和解决方案
V79.01 Hongmeng kernel source code analysis (user mode locking) | how to use the fast lock futex (Part 1) | hundreds of blogs analyze the openharmony source code
What are the differences between the architecture a, R and m of arm V7, and in which fields are they applied?
VSYNC+三重缓存机制+Choreographer
Serial port to WiFi module communication
Installing Oracle EE
Personal decoration notes
目标检测的yolov3、4、5、6总结
Principle and application of single chip microcomputer - principle of parallel IO port
Input标签的type设置为number,去掉上下箭头
Differences among tasks, threads and processes
任务、线程、进程 区别
Shell脚本-case in 和正则表达式
NFT监管要点和海外政策
Foundation: 2 The essence of image