Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand BASH in shell programming

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to understand BASH in shell programming". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Know Bash

Communicate the input instructions with Kernel through Shell, and let Kernel control the hardware work.

View Shell:cat / etc/shell

The default shell type after login is recorded in / etc/passwd and is related to the account.

Command alias: alias lm='ls-al' # Note whether there are spaces

Help: man bash

Command type: type

The code is as follows:

Type ls # ls is aliased to `ls-color=auto'

Type cat # cat is hashed (/ bin/cat)

Type cd # cd is a shell builtin

Command execution:

\ [Enter]: escape [Enter], press and do not execute, but start a new line

2. Variable function of Shell

Concept: different users can have different values for the same variable, so you can use variables to represent this value instead of writing specific values to the program, such as $PATH

The code is as follows:

Display variable: echo $PATH

Set variable: PATH=xxx # Note whether there are spaces or not

Extended variable: PATH=$PATH:xxx

Cancel variable: unset MAIL

View environment variables: env

View environment variables and custom variables: set

PS1: prompt character settin

Custom variable to environment variable: export

Why switch to environment variables: because the subroutine inherits the environment variables of the parent program

Language family support

View the languages supported by the system: locale-a

Check out the variables related to the language family: locale

Keyboard reads user input read: read atest # reads user input into the atest variable

Declare variable type declare: declare-I sum=100+50

3. Command aliases and historical commands

Alias

The code is as follows:

Alias lm='ls-al | more'

Unalias lm

Historical command

The code is as follows:

History

4. The operating environment of Bash shell

Search order when instruction is executed

Relative / absolute path-> alias-> bash built-in-> $PATH

Bash profile

The code is as follows:

Login shell/non-login shell

Login shell: you need to enter your account password when you enter shell

Non-login shell: log in through the graphical interface, and then start shell without entering the account password

Configuration file

The code is as follows:

Login shell:

/ etc/profile: overall system settings

The following three files are in order. If they exist, they are read, and the following files are no longer read.

~ / .bash_profile

~ / .bash_login

~ / .profile

Wildcard character

The code is as follows:

*: 0 to infinite characters

?: one character

[]: [abc] # an or b or c

[-]: [0-9] # 0 to 9

[^]: [^ abc] # is not a character of arecoverbmenc

5. Data stream redirection

The code is as follows:

>: overwrite

> >: accumulate

/ dev/null: receives information but does not display it

Execute in turn: cmd1,cmd2,cmd3

Determine the next one according to the implementation result of the previous one

Cmd1 & & cmd2

Cmd1 | | cmd2

6. Pipeline command

The code is as follows:

Ls | less

Cut: data segmentation in the same row

Echo $PATH | cut-d':'- f 5 # split by':', the fifth data

Grep: if there is any information in the line, return this line

Sort: sorting in

Uniq: repeated content is displayed only once

Wc: counting words

Tee: bidirectional redirection

Tr: delete or replace data

Col: converting to plain text

Join: put the relevant information in two files together

Paste: merge the two lines together with [Tab] in the middle

Expand: turn [Tab] into a space

Split: transfer large files to small files

That's all for "how to understand BASH in shell programming". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report