Shell Getting Started Note 2: string, array, echo, and printf

Source: Internet
Author: User
Tags echo command

Description

This is a note about http://c.biancheng.net/cpp/shell/.

Shell String

Strings can be in single or double quotes, or without quotation marks.

1 #!/bin/bash 2  3 # #字符串 4  5 #不加引号: 6 #无法出现; 7 echo a 8 echo a;b 9 #单引号: One #单引号里的任何字符都会原样输出, variable substitution in single-quote strings is not valid; 12 #单引号字符串中不能出现单引号 (not even after using the escape character for single quotes). Str= ' This is a string ' echo ${str}15 str_1= ' $str ' echo ${str_1}17 #str_2 = ' \ ' A ' #echo str_219 #双引号: #双引号里可以有变 #双引号里可以出现转义字符. Your_name= ' Wuyifan ' str= "Hello, I know that is" $your _name\ "!" echo ${str}26 #拼接字符串28 greeting= "Hello," $your _name "!" greeting_1= "hello,${your_name}!" echo $greeting "/" $greeting _131 #获取字符串长度33 string= "ABCD" Echo ${#string}35 #提取子字符串37 string= "Alibaba is a great Company "${string:1:4}39 Echo #查找子字符串: #索引42 #expr index $string $substring #在字符串 the $ The position of the first occurrence of the 1th character of a substring. The echo ' expr index ' $string ' is ' echo ' expr index ' $string ' at '
string

Shell Array

Bash supports one-dimensional arrays (which do not support multidimensional arrays) and does not limit the size of arrays.

Similar to the C language, the subscript of an array element is numbered starting with 0. Gets the elements in the array to take advantage of subscript, the subscript can be an integer or an arithmetic expression whose value should be greater than or equal to 0.

1 #!/bin/bash 2  3 # #定义数组 4  5 # () separated by a space or line break 6 names= ("LJQ" "YMQ" "LC") 7 names_1= (8 "LJQ" 9 "YMQ" ten "LC" one) #还 Each component of an array can be individually defined names_2[0]= "LJQ" names_2[1]= "Ymq", "names_2[2]=", "LC" #可以不使用连续的下标, and the subscript has no limit on the range of the subscripts # #读取数组19 echo " First name: ${names[0]} "echo" Second name: ${names[1]} "echo" Third name: ${names[2]} "#使用 @ or * can get all the elements in the array echo ${ names_1[*]}24 Echo ${names_2[@]}25 # #获取数组的长度: Same length as get string #获取数组元素的个数28 echo ${#names [*]}29 echo ${#names [@]}30 # Gets the length of an array of individual elements echo ${#names [2]}
Array

echo command vs. printf command

Echo is an internal command of the shell that prints the specified string on the screen.

The printf command is used to format the output and is an enhanced version of the echo command. It is a finite variant of the C-language printf () library function, and is somewhat different in syntax.
Note:

printf does not wrap like echo, and you must explicitly add a line break (\ n).
The syntax of the printf command:
printf format-string [arguments ...]
Format-string is the format control string and arguments is the parameter list.

This is only a description of the difference from the C-language printf () function:
The printf command does not have parentheses.
Format-string can be without quotation marks, but it is best to add single quotes and double quotes.
When the parameter is more than the format control (%), format-string can be reused and all parameters can be converted.
Arguments use spaces separated by commas.

1 #!/bin/bash 2  3 #format-string double quotes 4 printf "%d%s\n" 1 "ABC" 5  6 #单引号与双引号效果一样  7 printf '%d%s\n ' 1 "abc" 
    8  9 #没有引号也可以输出10 printf%s abcdef11 echo12 #格式只指定了一个参数, but the extra parameters will still output in that format, format-string be reused in printf%s ABC Def15 EC Ho16 printf "%s\n" ABC def17 printf "%s%s%s\n" a b c D e F g h i j18 #如果没有arguments, then%s is replaced with NULL,%d replaces with 0 printf "%s a nd%d \ n "#如果以%d format to display the string, then there will be a warning that the invalid number, at this time default to 023 printf" The first program always prints '%s,%d\n ' "Hello Shell
Echo and printf

Shell Getting Started Note 2: string, array, echo, and printf

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.