Linux (v) Shell Programming Basics

Source: Internet
Author: User

I. Introduction to Linux Shell

1. Shell overview

The shell is an interface that the user interacts with the kernel, and the most popular shell is now called the Bash shell
The shell is a programming language < interpreted type of programming language, that is, Shell script < is in the Linux shell command programming;
The Shell is a scripting language, so you have to have an interpreter to execute the scripts.
The common shell scripting interpreters on Unix/linux include bash, sh, csh, Ksh, and so on, which are used to call them a shell.
We often say that there are many kinds of shells, in fact, the shell script interpreter, you can use the Cat/etc/shells command to view the shell installed in the system, different shell may support the command syntax is not the same
2. Shell Basic Format

The code is written in a plain text file, usually ending with a. sh

VI helloworld.sh

#!/bin/bash # # Indicates which Shell parser to use to parse the execution of our script
echo "Hello World" # # note can also be written here
Save to exit
Here we write a shell script, the first line is fixed required, which indicates which Shell parser to use to execute our script. Essentially, the code inside the shell script is the Process Control statement plus some special syntax plus shell commands. Among them, we can consider each command as a key word in shell programming.
3. Shell Execution mode

(1) SH mode

SH helloworld.sh # # directly specified with the system default Bash shell interpretation execution

(2) Source mode

The source command, also known as a Point command, is a dot symbol (.), which is the internal command of bash.
Function: Causes the shell to read into the specified shell program file and execute all the statements in the file sequentially. The source command is typically used to re-execute the initialization file you just modified so that it takes effect immediately without having to log off and log back on.
Usage: source filename or. filename Example: Source helloworld.sh or. helloworld.sh

Note: There is a space between the point and the script

(3) Execute the script file directly

There are two ways to do this, but the execution of both will require that the file has execute permissions, so before we do, we need to change his execution permissions.
chmod 755 helloworld.sh

Switch to the path where the file is located execute the command:./helloworld.sh or helloworld.sh

Execute directly in absolute path:/root/bin/helloworld.sh

Second, Shell basic syntax

1. System Variables

Variables in the Linux Shell are divided into "system variables" and "user-defined variables"
System variables can be used to view common system variables through the SET command: $PWD $SHELL $USER $HOME

2. Custom variables

(1) Syntax:

          variable = value (e.g. STR=ABC)      equals cannot have spaces on both sides       variable names are generally used in uppercase
&N Bsp       &NBSP;&NBSP,
          using variables: $STR
          Double There is a difference between the quotation mark and the single quotation mark:  double quotation marks will only take the space,  single quotation marks will refer to the variables such as $param
          (2) Example:
          &NBSP;

Explain:
Command: Abc=huang Bo, when defining a variable with a space in the middle, then must be quoted, otherwise it will not be defined successfully
Command: abc= ' Huang Bo ' with single quotation marks as output. Indicates that the value in the middle of the quotation mark is the whole string in quotation marks to refer to the variable, single and double quotation marks are different:
Command: Echo ' Xu Zheng $ABC ' and echo "Xu Zheng $ABC"
Please see the difference:
In the case of single quotes, anything in the quotation marks is treated as a string, meaning that special characters are
If it is a double quote, then $ABC can print out the value of the variable

If the order is this: echo "Xu Zheng $ABCabc", can I also print out the value of the variable ABC? Please see the results:

(3) Advanced usage of variables          

Undo Variable: Unset ABC
Declaring a static variable: readonly abc= ' ABC ' is characterized by the variable being read-only and cannot be unset
Please take a look at an example, I now write two scripts, call b.sh execution in a.sh, then we want to know if a script can get to the B script variable, b script can get to a script variable?

Linux (v) Shell Programming Basics

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.