Linux Learning Notes (*) Shell Basics Bash Basic Features

Source: Internet
Author: User
Tags aliases clear screen control characters delete key echo command save file syslog

1 shell overview

The shell is a command interpreter that provides the user with an interface system-level program that sends a request to the Linux kernel to run the program. The user can start, suspend, stop, or even write some programs with the shell.

The shell is a powerful programming language that is easy to write, easy to debug, and highly flexible. The shell is a scripting language that interprets execution and can invoke Linux system commands directly.

The shell is usually divided into two categories: B Shell and C shell. The former main file name is sh, the latter is mainly used in BSD version of UNIX, its syntax format and C language similar. These two shell syntaxes are incompatible with each other. Bash is compatible with SH, and now Linux is the basic shell for users using bash.

Linux-supported shells can view the/etc/shells configuration file:

[Email protected] ~]# Cat/etc/shells

/bin/sh

/bin/bash

/sbin/nologin

/bin/dash

/bin/tcsh

/bin/csh

You can convert to each other by entering their respective commands, such as using sh:

[[Email protected] ~]# sh

sh-4.1#

sh-4.1# exit

Exit

[Email protected] ~]#

2 How shell scripts are executed

1) The echo command is used to output information in the following format:

echo [options] [Output CONTENT]

Where the-e option supports character conversions for backslash control. Common control characters are shown in the following table:

Control characters

Role

\\

Output \ Itself

\a

Output warning tone

\b

Backspace key, or left delete key

\c

Cancels the line break at the end of the output line, consistent with the "-N" option

\e

Escape key

\f

Page break

\ n

Line break

\ r

Enter

\ t

tab, which is the TAB key

\v

Vertical tab

\0nnn

Output characters according to the Octal ASCII table, where 0 is a digital 0,nnn is a three-bit octal number

\xhh

Output sub-non-fish according to the hexadecimal ASCII table, where HH is a two-digit hexadecimal number

Example: Delete the left character:

[Email protected] ~]# echo-e "Abcd\be"

ABCe

tab and line break:

[Email protected] ~]# echo-e "A\TB\TC\ND\TE\TF"

A b C

D E F

Hexadecimal ASCII code output:

[Email protected] ~]# echo-e "\x61\t\x62\t\x63\n\x64\t\x65\t\x66"

A b C

D E F

Output color:

[Email protected] ~]# echo-e "\e[1;31m ABCD \e[0m"

Abcd

where "\e[1" means the color output is turned on, and "\e[0m" indicates the end color output.

30m= Black 31m= red 32m= green 33m= Yellow

34m= Blue 35m= Magenta 36m= cyan 37m= White

2) The first script, where all lines are shell flags, indicates that the following write program is a shell script, followed by a # notation

[Email protected] ~]# VI hello.h

#!/bin/bash

#Description: The first program

#Autor: WS

#Date:

echo "Hello World"

3) Script execution

① Execute script via bash call

[Email protected] ~]# bash hello.h

Hello World

② gives execute permission to run directly (custom execution mode)

[Email protected] ~]# chmod 755 hello.h

[Email protected] ~]#./hello.h

Hello World

Extension: The return character of Linux is indicated by "$", it can be viewed by cat–a, and the return character of Windows is displayed as "^m$" in Linux, which needs to be converted using Dos2unix command.

3 Basic features of Bash (1) History command and command completion

1) Historical command history, in the form of:

History [Options] [Historical command Save file]

Where the-C option indicates the Purge history command (generally not recommended), and-w means that the history commands in the cache are written to the command cache file ~/.bash_history.

The history command saves 1000 records by default and can be changed in the environment variable profile/etc/profile, in the location: histsize=1000.

Historical commands are invoked in the following ways:

① use the up and down arrows to invoke

② using "!n" to perform the nth historical command

③ use "!!" Repeat the previous command

④ use "! string" to repeat the last command that starts with the string

2) in bash, command and file completion can be done through the "tab" key.

(2) command aliases and common shortcut keys

1) Command aliases

The format of the command alias is: alias alias = "Original Command"

The way to query a command alias is: Alias

Note the order in which commands are executed when using aliases:

① the first bit is the command executed with absolute or relative path;

② second Shun bit is the execution alias;

③ third step is to execute the internal command of bash;

④ the first command found in order to perform the directory lookup sequence defined by the $PATH environment variable.

You can view the system's environment variables through $path:

[Email protected] ~]# $PATH

-bash:/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin: No file or directory

Note: The alias of the command line definition is temporary and, if it is to be permanent, write an alias in the. BASHRC of the home directory, such as the alias file under root:

[Email protected] ~]# VIM/ROOT/.BASHRC

#. BASHRC

# User specific aliases and functions

Alias rm= ' Rm-i '

Alias cp= ' Cp-i '

Alias mv= ' Mv-i '

# Source Global Definitions

if [-F/ETC/BASHRC]; Then

. /etc/bashrc

Fi

~

The command to delete an alias is: Unalias alias.

2) bash common shortcut keys

Shortcut keys

Role

CTRL + A

Move the cursor to the beginning of the command line, if the command you entered is too long, you want to move the cursor to the beginning of the command line using the

Ctrl+e

Move the cursor to the end of the command line

CTRL + C

Force the current command to terminate

Ctrl+l

Clear screen, equivalent to clear command

Ctrl+u

The command before the cursor is deleted or clipped. Input a long line of command, do not use backspace key one character Delete, use this shortcut key more convenient

Ctrl+k

Delete or cut the contents after the cursor

Ctrl+y

Paste Ctrl+u or ctrl+k-cut content

Ctrl+r

Search in the history command, the search screen will appear when you press the shortcut, and you will search from the history command as soon as you enter the search content.

Ctrl+d

Exit Current Terminal

CTRL + Z

Pause, and put in the background, the shortcut is involved in the work of management content

Ctrl+s

Pause Screen Output

Ctrl+q

Restore Screen Output

(3) input/output redirection

1) standard input and output

Equipment

Device file name

File descriptor

Type

Keyboard

/dev/stdin

0

Standard input

Display

/dev/stdout

1

Standard output

Display

/dev/stderr

2

Standard error Output

2) Output redirection

Type

Symbol

Role

Standard output redirection

Commands > Documents

Output the correct output of the command to the specified file or device in a covered manner

Commands >> Documents

Output the correct output of the command to the specified file or device in an additional way

Standard Error Output redirection

Error command 2> file

Output the error output of the command to the specified file or device in a covered manner

Error command 2>> file

Output the error output of the command to the specified file or device in an additional way

Correct output and error output are saved simultaneously

Commands > Documents 2>&1

Save the correct output and error output to the same file in a covered manner

Commands >> Documents 2>&1

Save the correct output and error output to the same file in an additional way

Commands &> Documents

Save the correct output and error output to the same file in a covered manner

Commands &>> Documents

Save the correct output and error output to the same file in an additional way

Commands >> files 1 2>> file 2

Append the correct output to the file 1 and append the error output to file 2

Example 1: Write the output of the LS command to the Test1 file:

[[email protected] ~]# ls > test1

[email protected] ~]# cat Test1

Anaconda-ks.cfg

Hello.h

Install.log

Install.log.syslog

Ntfs-3g_ntfsprogs-2014.2.15.tgz

Test1

Testfile

Example 2: Append the output of the date command to Test1:

[[Email protected] ~]# date >> test1

[email protected] ~]# cat Test1

Anaconda-ks.cfg

Hello.h

Install.log

Install.log.syslog

Ntfs-3g_ntfsprogs-2014.2.15.tgz

Test1

Testfile

Saturday, February 21, 2015 04:29:05 CST

Example 3: Write the error output to the Test2 file:

[email protected] ~]# lis 2>> test2

[Email protected] ~]# LST >> test2 2>&1

[[Email protected] ~]# date &>> test2

[email protected] ~]# cat Test2

-bash:lis:command not found

-bash:lst:command not found

Saturday, February 21, 2015 04:37:31 CST

Note: The unwanted output is generally dropped directly into the bin, the corresponding command is: LS &>/dev/null

3) Input redirection

The WC command is a statistical command in the form of:

WC [options] [filename]

Where the-c option represents the number of statistics bytes, the-W option indicates the number of words, and the-l option represents the count rows

The general format for input redirection is: command < file, which indicates that the file is entered as a command, such as:

[Email protected] ~]# WC < anaconda-ks.cfg

54 124 1272

(4) Sequential execution of multiple commands and pipe characters

1) Multiple command execution order

Multi-Command Executor

Format

Role

;

Command 1; command 2

Multiple command order execution, no logical connection between commands

&&

Command 1&& Command 2

Logically with, when command 1 is executed correctly, command 2 does not execute, and command 2 does not execute when command 1 is executed incorrectly

||

Command 1| | Command 2

Logical OR, command 2 does not execute when command 1 is executed correctly, and command 2 does not execute when command 1 is executed correctly

Cases:

[Email protected] ~]# ls;date;cd/usr/;p WD

Anaconda-ks.cfg Install.log ntfs-3g_ntfsprogs-2014.2.15.tgz Test2

Hello.h Install.log.syslog test1 Testfile

Saturday, February 21, 2015 04:51:56 CST

/usr

The DD command is used for disk replication or data replication in the form of:

DD if= input file of= output file bs= number of bytes count=

Where the if= input file specifies the source file or the source device, the of= output file specifies the target file or target device, bs= bytes indicates how many bytes of input/output at a time, that is, these bytes as data blocks, count= number of input/output number of data blocks

Example: Write the contents of/dev/zero to the/root/testfile file, write 100M together, and display the access time:

[Email protected] usr]# date;dd if=/dev/zero of=/root/testfile bs=1k count=10000;date

Saturday, February 21, 2015 05:01:43 CST

Recorded 10000+0 read-in

Recorded the writing of 10000+0.

10240000 bytes (Ten MB) copied, 1.29893 sec, 7.9 mb/sec

Saturday, February 21, 2015 05:01:44 CST

[Email protected] usr]# ll-h/root/testfile

-rw-r--r--. 1 root root 9.8M February 05:01/root/testfile

Note: To determine whether a command is executed correctly, you can use the following command format:

Command && echo yes | | Echo No

2) Pipe character

The command format for the pipe character is:

Command 1 | Command 2

Represents the correct output of command 1 as the Action object of command 2

Example: Use the more command to display the details of a file in the/etc directory:

[Email protected] usr]# Ll-a/etc/| More

Total dosage 1796

Drwxr-xr-x. 103 root root 12288 February 21 03:59.

Dr-xr-xr-x. Root root 4096 February 21 03:59.

Drwxr-xr-x. 3 root root 4096 January 7 22:12 ABRT

Drwxr-xr-x. 4 root root 4096 January 7 22:22 ACPI

-rw-r--r--. 1 root root 44 February 20:44 Adjtime

......

The grep command is used for searching, and its command format is:

grep [Options] "Search content" file name

Where the-I option indicates that the case is ignored, the-n option represents the output line number, and the-V option indicates a reverse lookup, and the--color-auto option indicates that the search-out keyword is displayed in color.

(5) wildcard characters and other special symbols

1) wildcard characters

Wildcard characters

Role

?

Match an arbitrary character

*

Match 0 or any number of characters, which is what matches anything

[]

Match any one of the characters in the brackets

[-]

Matches any one of the characters in the brackets-representing a range, such as [A-z] for matching a lowercase letter

[^]

Logical non, which means that a match is not a character within the brackets, such as [^0-9] represents a character that is not a number

2) Other special symbols in bash

Symbol

Role

‘ ‘

Single quotes, all special symbols in single quotes, such as "$" and "'" (anti-quotes) have no special meaning

" "

Double quotes, special symbols in double quotes have no special meaning, but "$", "" "and" \ "are exceptions, with special meanings of" value of the calling variable "," Reference command ", and" escape character ", respectively.

` `

The anti-quote, anti-quote content is a system command that will be executed first in bash, and the same as $ (), but $ () is recommended, because the anti-quote is easy to see wrong

$()

As with the anti-quote function, used to refer to system commands

#

In the shell script, #开头的行代表注释

$

The value that is used to invoke the variable, such as when the value of the variable name needs to be called, the value of the variable needs to be $name

\

The escape character, followed by a special symbol after \ will lose its special meaning and become a normal character. such as \s will output the $ symbol, not as a variable reference

Example: Single and double quotes:

[Email protected] usr]# NAME=WS

[Email protected] usr]# echo "$name"

Ws

[Email protected] usr]# echo ' $name '

$name

[[email protected] usr]# echo "$ (date)"

Saturday, February 21, 2015 05:29:24 CST

[[email protected] usr]# echo ' $ (date) '

$ (date)

Example: Anti-quote:

[[email protected] usr]# abc= ' Date '

[Email protected] usr]# echo $ABC

Saturday, February 21, 2015 05:31:57 CST

[[email protected] usr]# abc=$ (date)

[Email protected] usr]# echo "$ABC"

Saturday, February 21, 2015 05:32:28 CST

Linux Learning Notes (*) Shell Basics Bash 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.