Bash study notes: foundation

The Bash shell is commonly used interactively: It lets you enter and edit commands, then executes them when you press the Return key.The Bash shell can also be run non-interactively from a script, making the shell require no human interaction.

variable

Initialize variables, Bash shell does not distinguish between types of variables in default, even if the integer and fractional assigned to variables, they will be treated as strings.

Use variable is referenced by the dollar sign $. E.g

#! / usr / bin / the env the bash 

author = " Tom " 
echo $ author
 echo $ {author} 

# The result is assigned to the command variable 
variable = $ (Command) 

echo $ {variable}

Variable name outside the curly braces are optional and can be added from time to increase, together with border interpreter can help identify the variables. Advice to all variables braces.

Variables defined in the shell, the default is the global variable. Scope of global variables is the current shell process, instead of the current shell script file.

Variables defined in the default shell functions is also a global variable, if you want the variable scope is limited to internal functions that can be combined with local command when you define.

Global variables are only valid in the current Shell process, other Shell and child processes are ineffective. If you use the export command to export a global variable, then it is also valid in all sub-processes, and this is called environment variables.

Exported by export environment variables Shell only for the current process and all child processes effective, if the top-most parent process was closed, then the environment variable was also gone, others will not be able to use the process, so that the environment variables are also temporary.

Command substitution

shell command substitution refers to the output of the command is assigned to a variable. There are two ways to complete the command substitution, one backticks `` the second is the dollar sign $ parentheses ().

variable=`commands`
variable=$(commands)

commands is the command to be executed. commands may be only one command, there may be a plurality of commands to command a plurality semicolon; separated.

$ () Is valid only in the Bash shell, but supports nested. Back quotes `` can be used in a variety of shell, but does not support nested.

parameter

$ N of the form to receive parameter called the position parameter.

Positional parameters passed to the script file

echo "Language: $1"
echo "URL: $2"

 Positional parameters passed to the function

#-Defined function
 function FUNC () {
     echo  " Language: $ 1 " 
    echo  " the URL of: $ 2 " 
} 
# calling a function 
FUNC the Java HTTPS: // www.company.com

There are some special shell variables:

  • $ 0 Current file name of the script
  • $ # Number of arguments passed to the script or function
  • $ * All arguments passed to the script or function
  • $ @ All the parameters passed to the script or function
  • $? Exit status of the last command or function's return value
  • $$ current shell process ID. For shell scripts, these scripts are located is the process ID

the shell variables into the position variables and special variables.

type of data

basic data types shell strings, arrays.

Math

shell and other programming in different languages, shell can not perform arithmetic directly, you must use the math commands. the shell common mathematical calculation command follows:

  • (()) For the integer arithmetic, is very efficient, it is recommended to use
  • let for integer arithmetic
  • $ [] For integer arithmetic
  • expr for integer arithmetic, string can be processed
  • bc can handle both integer and fractional, a calculator program under Linux
  • declare -i The variable is defined as an integer, supports only the most basic mathematical operations (addition, subtraction and modulo)

 Bis parentheses (()) in the syntax is: ((expression)) may be only one expression, may be a plurality of, among a plurality of expressions separated by commas. For a plurality of expressions to the value of the last expression as the execution result of the whole (()) command.

You can use $ get (()) command results, and use it to obtain $ variable value is similar. In (()) variables you do not need to add $ prefix (()) will automatically resolve the variable name.

b=10
((a=b+5))
echo ${a}

d=$((b+6))
echo ${d}

 

let command syntax is: let or let the expression "expression" or let 'expression' both are equivalent to ((expression)).

Select structure

The most commonly used if statement syntax is:

if  condition
then
    statement(s)
fi

 

And then also if you can write a single line:

if  condition;  then
    statement(s)
fi

 

Multi-conditional statement:

if  condition1
then
   statement1
elif condition2
then
    statement2
elif condition3
then
    statement3
...
else
   statementn
fi

 

Exit status exit statu each shell commands, including Bash built-in commands, Linux commands, custom shell functions, when it exits (end of run), will return to a relatively small integer value to the calling (use) of its program, this is the exit status of the command.

Many Linux command is actually a C language program, the last main () function has a return 0, if the program wants to quit in the middle, you can also use exit 0, this is in fact the exit status of the C language. When there are other programs call this program, you can capture the exit status.

Determine if statement's condition, in essence, is to determine the command exit status.

By convention, the introduction of the state of 0 indicates success, any other exit status as failed. But there are exceptions, such as the diff command.

 

INSERT

 

reference:

http://c.biancheng.net/shell/ 

Guess you like

Origin www.cnblogs.com/colin220/p/10967316.html