当前位置:网站首页>Shell syntax

Shell syntax

2022-06-25 21:15:00 dengk2013

#!/bin/bash

meaning : Set up current shell The script file adopts bash The parser runs the script code

Special variables :$n

Used to receive the parameters passed in when the script file is executed

$0 Used to get the name of the current script file
$1~$9, Means to obtain the first input parameter to the 9 Three input parameters
The first 10 Format of more than input parameters : ${ Numbers }, Otherwise, you can't get

Special variables :$#

grammar

$#

meaning

Get the number of all input parameters

Special variables :$?

grammar

$?

meaning

Used to get previous Shell Exit status code of the command , Or the return value of the function

Every Shell Command execution has a return value , This return value is used to indicate whether the command execution was successful

Generally speaking , return 0 Indicates that the command was executed successfully , Not 0 Delegate execution failed

A string of 3 Format differences

  1. Use single quotes '' String :

    Any character will be output as it is , Using variables in it is not valid

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 .

Double quoted substrings can also appear in the string , But you need to escape

3.

${# String variable name }

4.

String truncation Syntax

Format explain
${ Variable name :start:length} from string To the left of the string start Character start , Intercept to the right length Characters .
${ 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 .
${ 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 of a string *chars The position begins , Intercept *chars All characters on the right .
${ Variable name ##*chars} from string The last occurrence of the string *chars The position begins , Intercept *chars All characters on the right .
${ Variable name %chars*} from string The last occurrence of the string chars* The position begins , Intercept chars* All the characters on the left .
${ Variable name %%chars*} from string The first occurrence of a string chars* The position begins , Intercept chars* All the characters on the left

alias Alias =' command '  You can specify rm -rf Alias to prevent deletion

unalias Alias

echo -e ' The string contains escape characters '

#!/bin/bash
read -p " Please enter a name , Age , hobby : " name age hobby
echo " full name :$name"
echo " Age :$age"
echo " hobby :$hobby"

原网站

版权声明
本文为[dengk2013]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202181332392489.html