当前位置:网站首页>Shell string variable
Shell string variable
2022-06-25 13:59:00 【Resounding through heaven】
1 Introduce
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 )
2 Three formats of string
1 Single quotation mark method
2 Double quotation marks , recommend
3 No quotation marks
vim string.sh
#!/bin/bash
name1='james'
name2="kobe"
name3=paul
echo "name1: ${name1}"
echo "name2: ${name2}"
echo "name3: ${name3}"
3 The three formats of strings differ
1 Use single quotes ’' String
Any character will be output as it is , Using variables in concatenated strings is invalid .
2 By double quotation marks " "
Surrounded string
It contains variables , Then the variable will be parsed to get the value , Instead of outputting it as it is .
3 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 commands .
demo:
vim string.sh
#!/bin/bash
name="kobe"
echo ' Print with single quotation marks name: ${name}'
echo " Use double quotation marks to print name: ${name}"
echo Do not use quotation marks for printing name:${
name}
4 Get string length
4.1 grammar
${# String variable name }
4.2 meaning
Get string length
#!/bin/bash
name="kobe"
echo ' Print with single quotation marks name: ${name}'
echo " Use double quotation marks to print name: ${name}"
echo Do not use quotation marks for printing name:${
name}
echo " String length is : ${#name}"
4.3 result
5 String splicing
5.1 Splicing method
1 Unsigned splicing
2 Double quotation mark splicing
5.2 demo
#!/bin/bash
hello="hello"
world="world"
hello_world=${
hello}${
world}
echo " Unsigned splicing : ${hello_world}"
hello_world1="${hello}${world}"
echo " Double quotation mark splicing :${hello_world}"
6 String interception
6.1 grammar
Format | explain |
---|---|
${ Variable name :start:length} | from string To the left of the string start Character start , Intercept to the right length Characters .start from 0 Start |
${ Variable name :start} | from string To the left of the string start Characters begin to intercept , Until the last . |
${ Variable name :0-start:length} | from string To the right of the string start Character start , Intercept to the right length Characters .start from 1 Start , Represents the first character on the right |
${ Variable name :0-start} | from string To the right of the string start Characters begin to intercept , Until the last . |
${ Variable name #*chars} | from string The first occurrence to the left of the string *chars The position begins , Intercept *chars All characters on the right . |
${ Variable name ##*chars} | from string The last occurrence to the left of the string *chars The position begins , Intercept *chars All characters on the right . |
${ Variable name %chars*} | from string The first occurrence to the right of the string chars* The position begins , Intercept chars* All the characters on the left . |
${ Variable name %%chars*} | from string The last occurrence to the right of the string chars* The position begins , Intercept chars* All the characters on the left |
6.2 demo
character string "hello world"
From the left 0 Start , Intercept to the left 2 Characters
From the left 5 Start , Intercept all characters to the left
From the right 5 Start , Intercept to the right 2 Characters
Intercept the first character on the left l All characters on the right
Intercept the last character on the left l All characters on the right
Intercept the first character on the right l All the characters on the left
Intercept the last character on the right l All the characters on the left
#!/bin/bash
string="hello world"
echo " String is : ${string}"
str1="${string:0:2}"
echo " From the left 0 Start , Intercept to the left 2 Characters : ${str1}"
str2="${string:5}"
echo " From the left 5 Start , Intercept all characters to the left : ${str2}"
str3="${string:0-5:2}"
echo " From the right 5 Start , Intercept to the right 2 Characters : ${str3}"
str4="${string#*l}"
echo " Intercept the first character on the left l All characters on the right : ${str4}"
str5="${string##*l}"
echo " Intercept the last character on the left l All characters on the right : ${str5}"
str6="${string%l*}"
echo " Intercept the first character on the right l All the characters on the left : ${str6}"
str7="${string%%l*}"
echo " Intercept the last character on the right l All the characters on the left : ${str7}"
边栏推荐
- 触觉智能分享-RK3568在金融自助终端的应用
- Dialogue: recommended system quick start route and summary of knowledge points
- 测一测你的挣钱能力有多强?未来的你将从事什么职业?
- How to determine if a web worker has been created or closed
- 数据库中显示error1822,error1824
- Discuz仿今日头条模板/Discuz新闻资讯商业版GBK模板
- 数据采集系统网关采集工厂效率
- 请问通达信股票开户是安全的吗?
- The priority of catch() and then (..., ERR) of promise
- 语法'陷阱'
猜你喜欢
BACnet gateway bl103 for building automation
On the simple realization of Sanzi chess game and the method of judging the victory of n-zi chess
Explanation of a textbook question
一次性讲清楚 Handler 可能导致的内存泄漏和解决办法 | 开发者说·DTalk
“移动云杯”算力网络应用创新大赛火热报名中!
Les neuf caractéristiques de la parole et les neuf temps en anglais
Suanli & NFT trading platform f3 The exclusive NFT project of XYZ, hash eagle, will be grandly launched
Win7显示屏幕亮度在哪里可以调节
Dialogue: recommended system quick start route and summary of knowledge points
使用调试工具调试博图TCP连接所遇到的问题
随机推荐
Classifier and cross entropy loss function
DE2-115 FPGA开发板的VGA显示
Prototype and prototype chain - constructor and instanceof
Turtlebot+lms111+gmapping practice
K-line diagram 24 classic diagrams (shadow)
数据库中显示error1822,error1824
测一测你的挣钱能力有多强?未来的你将从事什么职业?
Hash table, hash conflict
通达信股票账户开户安全吗
Scope of ES6 variable
Explication d'un problème de manuel
sigmoid函数sigmoid求导
As a software testing engineer, how do you think to ensure software quality?
Data acquisition system gateway acquisition plant efficiency
NR-ARFCN和信道栅格、同步栅格和GSCN
腾讯云搭建Socks5多IP代理服务器实现游戏单窗口单IP完美搭建教程附带工具「建议收藏」
shell 运算符
多臺雲服務器的 Kubernetes 集群搭建
Rust,程序员创业的最佳选择?
删库跑路、“投毒”、改协议,开源有哪几大红线千万不能踩?