Home  >  Article  >  Operation and Maintenance  >  How to use bash commands

How to use bash commands

不言
不言Original
2019-01-23 15:36:128455browse

Bash (Bash) is the abbreviation of Bourne Again Shell, which is used to execute shells that describe commands (such as commands in Linux). In this article, we will introduce the use of bash commands.

How to use bash commands

Adopts bash as a standard on Linux, basically it describes the processing and execution of text such as vi editor with ".sh" extension.

Like programming, it has many functions such as variables, functions and arithmetic processing, so if you have a small program, you can write it in bash.

In addition, since bash is executed by the shell, it is also called a shell script.

Create a shell script

We first create a simple script that outputs "Hello World!!" to the console.

Use the vi command to create a new file.

$ vi hello.sh

After opening the editor, write as shown below.

#!/usr/bin/bash
echo "Hello World!!"
exit 0

The "#!/usr/bin/bash" on the first line means it is a shell script using bash.

The second line describes the statement to be executed.

Finally, use "exit 0" to exit bash. Parameter 0 indicates normal completion.

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, the commands when executing the shell script also include "." to change the execution permission. /"In progress.

$ chmod 755 hello.sh
$ ./hello.sh

There is a way to execute it with sh command.

$ sh hello.sh

Shell script can write comments and programming.

Comments can be written after "#".

#!/usr/bin/bash
echo "Hello World!!"
#结束处理。
exit 0

Shell scripts can define variables and assign values.

#!/usr/bin/bash
 
num=100
PI=3.14
STR1="Hello"
str_2="World!!"
 
echo ${num}
echo ${PI}
echo ${STR1}
echo ${str_2}
 
exit 0

Variables can be alphanumeric characters such as uppercase and lowercase letters, numbers and underscore (_).

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

Please note that if you put spaces before and after "=", it will cause an error.

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

Input and output

#!/usr/bin/bash
 
read AGE
echo "ege=$AGE"
 
exit 0

Execution results:

30
ege=30

read stores the content input from the console into the variable specified in the parameter.

The variables specified by read can be called ordinary variables.

The above is the detailed content of How to use bash commands. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn