#!/bin/bash
echo "Hello world!"
1. As an executable program
Save the above code as test.sh and CD to the appropriate directory:
chmod u+./test. #使脚本具有执行权限 ./test. #执行脚本
2. As an interpreter parameter
This works by running the interpreter directly, whose parameters are the file names of the shell scripts, such as:
/bin/sh test. SH/bin/php test. PHP
Using variables
Your_name= "QINJX" Echo $your _nameecho ${your_name}
echo "I am good at ${your_name}script"
ReadOnly modified to read-only
Unset variable_name
Single-Quote String restrictions:
- Any character in a single quotation mark is output as is, and the variable in the single-quote string is not valid;
- Single quotation marks cannot appear in single quote strings (not after using escape characters for single quotes).
Advantages of double quotes:
- You can have variables in double quotes.
- Escape characters can appear in double quotes
Stitching strings
Your_Name="QINJX"Greeting="Hello,"$your _name"!" greeting_1="Hello, ${your_name}!" echo $greeting $greeting _1
Get string length with #
String="ABCD"echo ${#string}#输出 4
String="Runoob is a great site"echo ${string:1:4}# output Unoo
String="Runoob is a great company"' expr index ' $string "is '# lookup is, output 8. "'" In the script is an anti-quote, not a single quote "'"
Array
Array_Name= (value0 value1 value2 value3)
Array_Name[0]=value0array_name[1]=value1array_name[n]= Valuen
Reading the value of an array
Valuen=${array_name[n]}
Use the @ symbol to get all the elements in the array, for example:
echo ${array_name[@]}
# Gets the number of array elements length=${#array_name[@]}# or length=${#Array_Name [*]}
Intercept string
1.#Remove the left character and leave the right character. Echo ${var#*//}where Var is the variable name,#number is an operator, *//means that the first//number and all characters on the left are deleted from the left.Delete http://The result is: www.aaa.com/123. htm2.## Intercept, delete the left character, and leave the right character. Echo ${var##*/}##*/indicates that the last (rightmost) one/number and all characters to the left are deleted from the left.That is, delete http://www.aaa.com/The result is123. htm3.%Remove the right character, leaving the left character Echo ${var%/*}%/* means starting from the right, deleting the first/and the character on the right, the result is: http://www.aaa.com4. PercentRemove the right character, leaving the left character Echo ${var%%/*}%%/* to start from the right and delete the last (leftmost) one/and the character on the right, the result is: http:5. Start with the first few characters on the left, and the number of characters echo ${var:0:50 means that the first character on the left begins,5represents the total number of characters. The result is: http:
Shell Programming Notes