Introduction to basic variables of Linux shell commands

Source: Internet
Author: User
Tags linux shell commands

Linux shell commands are inseparable from the programmer's life. However, many programmers are not able to use them skillfully. In fact, it is very easy to understand these things as long as they are familiar with their basic principles. Next, let's take a look at the specific Linux shell Command introduction.

In fact, the interactive interpretation and execution of user input commands as a command language is only one aspect of the shell function. Linux shell commands can also be used for program design, it provides a variety of methods to define variables and parameters as well as program control structures. Using shell programming is similar to a batch processing file in DOS, called a shell script, or a shell program or a Linux shell Command file.

Shell basic syntax

The basic syntax of shell is how to input commands to run programs and how to communicate between programs through shell parameters.

(1) Input/Output redirection

In Linux, each process has three special file description pointers: standard input (0), standard output, file description pointer is 1), standard error output (standard error, file description pointer is 2 ). These three special file description pointers enable the process to normally receive input from the standard input terminal and display the output from the standard terminal, linux also provides users with the ability to use common files or pipelines to replace these standard input and output devices. In Linux shell commands, you can use ">" and "<" to redirect input and output. For example:

Command> file: redirects the command output to a file.

Command> & file: redirects the standard error output of the command to a file.

Command> file: append the standard output result to the file.

Command >>> & file: append the structure of the standard output and standard error output to the file.

Command

(2) pipeline pipe

Pipe can also replace the standard input output and standard error output, so that the output of a program can be sent to the input of another program. The syntax is as follows:

 
 
  1. command1| command2[| command3...] 

It can also be sent to the pipeline together with the standard error output:

 
 
  1. command1| &command2[|& command3...] 

(3) foreground and background

Under the Linux shell Command, a new process can be executed by using the symbols ";" and "&" after the command respectively in the foreground and background. The syntax is as follows:

 
 
  1. command 

Generate a foreground process. The next command can only be entered after the command is run.

 
 
  1. command & 

Generates a background process that runs in the background and can input other commands.

Shell program variables and parameters

Like an advanced programming language, shell also provides instructions and the ability to use variables. For shell, the values of all variables are a string, and the shell program uses $ var to reference the value of the variable named var.

Shell has the following basic types of variables:

(1) environment variables defined by shell

The Linux shell Command has defined some variables related to the operating environment of the system at the beginning of execution. You can also redefine these variables. Common shell environment variables include:

HOME: the full path name used to save the registration directory.

PATH: used to save the directory PATH names separated by colons. shell searches these directories in the order given in the PATH variable. The first executable file with the same name as the command will be executed.

TERM: the type of the terminal.

UID: The identifier of the current user. The value is a string consisting of digits.

PWD: the absolute path name of the current working directory. The value of this variable varies with the use of the cd command.

PS1: Primary prompt. for privileged users, the default primary prompt is "#". For common users, the default primary prompt is "$ ".

PS2: When the Linux shell Command receives the user's input command, if the user enters "\" at the end of the input line, and then press enter, or, when the shell determines that the command entered by the user is not over when the user presses the Enter key, the pop-up prompt is displayed, prompting the user to continue entering the rest of the command. The default pop-up prompt is "> ".

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.