(1) Chapter One: Shell basic features

Source: Internet
Author: User
Tags shebang

1.1. After opening the terminal, a prompt will appear, usually in the following form:

[email protected]$

Or is:

[email protected] #

It is either simply expressed in $ or #.

($ represents a normal user, #表示超级用户 (root user)).

1.2. All scripting languages under Linux are #! followed by a special line called Shebang as the starting point of the interpretation authority path. Shell scripts often use #! /bin/bash start.

There are two ways to run a script: one is to use the script as a parameter to the SH command, and the other is to use the script as an executable file with Execute permissions.

The script is run as a parameter as follows:

$sh A.sh#假设脚本在当前目录下, when the first line is useless.

Or

$sh /home/a.sh #使用完整路径

Give script permission:

$chod a+x Acript.ah

Perform:

$./A.sh #./indicates the current directory, or you can use an absolute path

Execution procedure: The shell program reads the first line to see if the shebang line is #!/bin/bash. It recognizes/bin/bash and executes the script internally in the form of $/bin/bash a.sh.

1.3. A set of initialization instructions is executed when a terminal is opened, which is located in the. bashrc file (~/.BASHRC) in the home directory.

The ~./bash_history script is used to save commands that the user has run. ~ is a shorthand that represents the path of the user's home directory.

The commands are delimited by newline characters: $ cmd1;cmd2

Equivalent to $cmd 1

Cmd2

Note starts with # and continues to the end of the line

2, Terminal printing

2.1. Echo

Single quotes, double quotes, and no quotes can accomplish the same task:

$ echo "Welcome to bash"

$ Echo ' Welcome to bash '

$ echo Welcome to Bash

These three methods look similar, but each has some special uses and side effects.

The side effects of three methods are as follows:

    • You cannot use semicolons (;) in text without the quotation marks.
    • When you use double quotes, everything in the double quotation marks is printed as it is
    • When using single quotation marks, bash does not evaluate the variables in single quotation marks, but only displays them as they are. such as $ Echo ' $var ' will return $var, and $ echo $var or use double quotes will return its value based on whether the variable $var is defined or not.

This command has many parameters and must be described in one article.

2.2. printf

It's exactly the same as the C-language printf.

Add:

Flags in Echo and printf (such as-e,-N, and so on) should appear before any string within the command line, or bash treats it as another string.

In Echo Transit line break

By default, Echo appends a newline character to the end of the text, and using the-n flag allows you to omit the trailing line break.

Color Printing

Color text: reset = 0, black = 30, red = 31, green = 32, yellow = 33, blue = 34, Yang Hong = 35, cyan = 36, white =37

such as: Echo-e "\e[1;31m This is red text \e[0m"

\e[a;31m set the color to red, \e[0m the color back

Color background: reset = 0, black = 40, red = 41, green = 42, yellow = 43, blue = 44, Yang Hong = 45, cyan = 46, white =47

such as: Echo-e "\e[1;42m green background \e [0m]

3. Variables and environment variables

The variables in the scripting language are usually dynamic types, without the need for pre-declared types, directly assigned. The variables in bash are all strings. Some special variables are used by the shell and the operating system to store special values, namely environment variables.

ENV command: View the environment variables at runtime with each process:

cat /proc/#PID/environ #其中, sets the PID to the ID of the associated process (PID is always an integer).

ID get: Suppose a program called Gedit is running. You can use the Pgrep command to get the process ID of the gedit:

$ pgrep gedit12501

Use this

$ Cat /proc/12501/environ

The command will return a list containing the environment variable name and its corresponding variable value. Each variable is expressed in the form of Name=value, separated by a null character (+) and replaced with \ n, to display a pair of name=value in each row. The substitution is implemented with the TR command:

$ Cat /proc/12501/environ | TR ' / ' ' \ n '

4. Variable operation

Assignment Value: Var=value

If the value contains any white space characters, such as spaces, you must use either single or double quotation marks.

Note: Var0=value is not equal to var = value, the former is an assignment operation, and the latter is an equality operation.

You can print out the contents of a variable by adding a $ prefix before the variable name:

var="Jiang"echo $varecho ${var}

5. Environment variables

The Export command is used to set environment variables:

$ export path="$PATH:/home/user/bin"
#也可以使用 $  PATH
="$PATH:/home/user/bin"
$  Export  PATH

Any program executed from the current shell script inherits this variable. We can also follow our own needs, in the execution of the application or shell script everywhere the specific variables. By default, there are many variables to use.

Path is one of them:

$ echo $PATH

After giving the command to execute, the shell automatically finds the executable file in the directory list contained by the PATH environment variable, separated by a colon between the directory paths. $PATH is usually defined in/etc/environment or/etc/profile or ~/.BASHRC

There are also well-known environment variables: HOME, PWD, USER, UID, SHELL

6. Some skills

6.1 Get string length: length=${#var}

$ var=12345678901234567890
$    Echo
${#var}

6.2. Identify the current shell version

$ echo $SHELL /bin/ bash $ echo $0
Bash

6.3. Check whether it is a super user

UID is an important environment variable that can be used to check whether the current script is running as a superuser or as a normal user:

if [$UID-ne 0 ]; Then Echo Non root user. Please run as root. Else echo "Root user" fi #root用户的UID是0

6.4. Modify BASH Hint string

You can use the PS1 environment variable to customize the hint text. The default shell hint text is set on a line in the file ~/.BASHRC.

# Find out which line of PS1 is set to $ Cat ~/.BASHRC | grep PS1 PS1='${debian_chroot:+ ($debian _chroot)}\[email protected]\h:\w\$ '
#设置字符串, you can enter: $  PS1
="prompt>"

(1) Chapter One: Shell basic features

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.