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 use bash command in Linux system

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

It is believed that many inexperienced people have no idea about how to use the bash command in the Linux system. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Most Linux systems use the bash standard, which can process and execute files with ".sh" extension, which is also a kind of programming, but bash is parsed by sehll, so it is called shell programming.

Create a shell script

Let's start by creating a simple script that says "Hello World!!" Output to the console.

Use the vi command to create a new file.

After $vi hello.sh opens the editor, write as follows.

The "#! / usr/bin/bash" on the first line of #! / usr/bin/ bash``echo "Hello wordbacks!" ``exit 0 indicates that it is a shell script using bash.

The second line describes the statement to be executed.

Finally, exit bash using "exit 0". Parameter 0 indicates a normal end.

After creating the file, use the bash command to execute the shell script.

$bash hello.sh execution result:

Hello World!!Hello World!! Output

In addition, in addition to bash, commands for executing shell scripts are also run with ". /" to change the execution permissions.

$chmod 755 hello.sh`` $. / hello.sh has a way to execute it with the sh command.

The $sh hello.shshell script can write comments as well as programming.

Comments can be written after "#".

#! / usr/bin/ bash``echo "Hello wordbacks!" ``# ends processing The ``exit 0Shell script can define variables and assignments.

#! / usr/bin/ bash`STR1 100`Pi = 3.14``STR1 = "Hello" ``str_2= "Worldword!" ````echo ${num} ``echo ${PI} ``echo ${STR1} ``echo ${str_2} ````exit 0 variables can be alphanumeric characters, such as uppercase and lowercase letters, numbers and underscores (_).

When assigning a value to a variable, write it as variable = value.

Note that placing spaces before and after the "=" will result in an error.

In addition, when accessing a variable, you need to add ", such as {variable}" before the variable name, and enclose the variable in "{}".

Input and output

#! / usr/bin/ bash````read AGE``echo "ege=$AGE" ````exit 0 execution result:

30`ege = 30read stores input from the console to the variable specified in the parameter.

The variable specified by read can be called a normal variable.

What is Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

After reading the above, have you mastered how to use the bash command in the Linux system? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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