Linux Shell Learning

Source: Internet
Author: User

1.Shell itself is a program written in C language, it is a bridge for users to use Unix/linux, most of the user's work is done through the shell. the shell is both a command language and a programming language. as a command language, it interprets and executes user-entered commands interactively, and as a programming language, it defines variables and parameters and provides many control structures in high-level languages, including loops and branches.


2.the shell has two ways of executing commands:

    • Interactive (Interactive): Explains the execution of a user's command, the user enters a command, and the shell interprets the execution of a single rule.
    • Batch: The user writes a shell script in advance, with a number of commands that allow the shell to execute the commands at once without having to hit the command one at a time.
Shell scripts are similar to programming languages, and there are variables and process control statements, but shell scripts are interpreted and do not need to be compiled, and the shell program reads and executes the commands from a single line of script, which is equivalent to a user knocking a line of commands from the script to the shell prompt for execution.

3.bash is fully compatible with SH, meaning that a script written in SH can be executed without modification in bash.

4.Shell supports custom variables.

when defining a variable, the variable name does not have a dollar sign ($), such as:

 
   
  
  1. VariableName = "Value"
Note that there can be no spaces between the variable name and the equals sign (there are no spaces on either side), which may be different from any programming language you are familiar with. at the same time, the name of the variable names must follow the following rules:
    • You cannot have spaces between them, you can use underscores (_).
    • Punctuation cannot be used.
    • You can't use the keywords in bash (you can see the reserved keywords using the help command).
5. using a defined variable, simply precede the variable name with a dollar sign ($) , such as:
 
   
  
  1. your_name = "Mozhiyan"
  2. $your _name
  3. ${your_name} }
the curly braces outside the variable name are optional, plus the curly braces are used to help the interpreter identify the bounds of the variable , such as the following:
 
   
  
  1. for inch
  2. Do
  3. "I am Good at ${skill}script"
  4. Done
If you do not add curly braces to the skill variable and write the echo "I am good at $skillScript", the interpreter will treat $skillscript as a variable (whose value is null)and the result of the code execution is not what we expect it to look like.

It is a good programming habit to add curly braces to all variables.
6. Read-only variables
Use the readonly command to define a variable as a read-only variable, and the value of a read-only variable cannot be changed.

The following example attempts to change a read-only variable, resulting in an error:
 
   
  
  1. #!/bin/bash
  2. Myurl = "http://see.xidian.edu.cn/cpp/shell/"
  3. ReadOnly
  4. Myurl = "http://see.xidian.edu.cn/cpp/danpianji/"
run the script with the following results:
/bin/sh:name:this variable is read only.
7. Deleting variablesUse the unset command to delete a variable. Syntax:
 
   
  
  1. unset variable_name
The variable cannot be used again after it has been deleted; The unset command cannot delete a read-only variable.





Linux Shell Learning

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.