Linux Shell learns a

Source: Internet
Author: User

This blog is referenced from:

Http://www.cnblogs.com/waitig/p/5523409.html

"Shell from Beginner to mastery" Zhang Chunxiao

Shell Introduction

The shell itself is a program written in C, a user to use UNIX or Linux bridge, most of the user's work needs to be done through the shell. Only skilled use of the shell, you can master Linux.

It can be said: The shell is both a command language and a programming language.

Therefore, for the user, the shell is the most important utility program, in-depth understanding and proficiency in the shell characteristics of the extremely use of the method is the key to good unix/linux system.

It can be said that the proficiency of the shell is a reflection of the user's proficiency in the use of unix/linux.

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.

However, for Shell beginners, please note that in a normal application, do not run the shell with the root account . As a normal user, no matter whether you intentionally or unintentionally, can not destroy the system, but if it is root, it is different, as long as a few letters, can lead to disastrous consequences.

Common shell types

As mentioned above, the shell is a scripting language, so there must be 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.

Linux Gets the current shell environment:

As you can see, the default shell for the current system is bash. At the same time,/bin/sh is a symbolic link that points to/bin/bash.

Writing the first shell script
#!/bin/bashecho "Hello World!"

CD to the appropriate directory, execute the script as an executable, and modify the script to perform the permission.

One, variable

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

Variablename= "Value"

Note that there can be no spaces between the variable name and the equals sign, 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:

    • The first character must be a letter (a-z,a-z).
    • You can use an underscore (_) without spaces in the middle.
    • Punctuation cannot be used.
    • You can't use the keywords in bash (you can see the reserved keywords using the help command).

With a defined variable, just precede the variable name with a dollar sign ($), such as:

Your_name="paopao"echo  $your _nameecho ${your_name}

The curly braces outside the variable name are optional and add no lines, and curly braces are used to help the interpreter identify the bounds of the variable.

When you run the shell, there are three different variables:

1) Local Variables

Local variables are defined in a script or command, only valid in the current shell instance, and other shell-initiated programs cannot access local variables.

2) Environment variables

All programs, including shell-initiated programs, can access environment variables, and some programs require environment variables to keep them running properly. Shell scripts can also define environment variables when necessary.

3) Shell Special variables

Shell variables are special variables that are set by the shell program. Some of the shell variables are environment variables, some of which are local variables that guarantee the shell's normal operation. The following is a description of the special variables in the shell.

Shell Special variables

The special variables in the Shell are mainly as follows:

$ A, $#, $*, [email protected], $?, $$

The $n represents the nth parameter passed to the script $# the number of command-line arguments. The name of the current script $* returns the value of all parameters in the form "parameter 1 Parameter 3 ..." [Email protected] returns $ in the form of each parameter string. is a system variable used to get the execution state of the shell command, and the execution returns 0 successfully.
#!/bin/BashEcho "file name: $"Echo "first parameter: $"Echo "The second parameter: $ A"Echo "all parameters: [email protected]"Echo "all parameters: $*"Echo "Number of parameters: $#"

Linux Shell learns a

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.