Detailed explanation of bash command usage

Detailed explanation of bash command usage

On Linux, bash is adopted as the standard, which basically describes the processing and execution of text with a ".sh" extension like the vi editor.

As with programming, it has many features like variables, functions, and arithmetic processing, so if you are making a small program, you can write it in bash.

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

Create a shell script

We start by creating a simple script that prints "Hello World!!" to the console.

Use the vi command to create a new file.

$ vi hello.sh

Once the editor opens, write as shown below.

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

The "#!/usr/bin/bash" on the first line 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 normal termination.

After creating the file, execute the shell script using the bash command.

$ bash hello.sh

Execution Result:

Hello World!!

Hello World!! is output

In addition, when executing shell scripts, in addition to bash, there are also commands that change execution permissions and run with "./".

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

There is a way to execute it using sh command.

$ sh hello.sh

Shell scripts can be used for commenting as well as programming.

Comments can be written after "#".

#!/usr/bin/bash
echo "Hello World!!"
#End processing.
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 underscores (_).

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

Note that if you put spaces before and after the "=", 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 in "{}".

Input and Output

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

Execution Result:

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.

You may also be interested in:
  • Bash script enables you to view Linux system information every time you log in to the shell
  • How to execute Linux Bash commands in Python3
  • Detailed explanation of how to pass password to ssh/scp command in bash script
  • Summary of Creating and Using Array Methods in Bash Scripts
  • A Deep Understanding of Angle Brackets in Bash (For Beginners)
  • Implementation of Java code executing shell commands
  • Java simple implementation of calling command line and getting execution result example
  • Example of calling shell command in java and getting the execution result
  • Implementing bash command process parsing through Java

<<:  MySQL 5.7.15 installation and configuration method graphic tutorial (windows)

>>:  JS implementation of Apple calculator

Recommend

How to deploy a simple c/c++ program using docker

1. First, create a hello-world.cpp file The progr...

Nginx builds rtmp live server implementation code

1. Create a new rtmp directory in the nginx sourc...

In-depth explanation of hidden fields, a new feature of MySQL 8.0

Preface MySQL version 8.0.23 adds a new feature: ...

MySQL database migration quickly exports and imports large amounts of data

Database migration is a problem we often encounte...

FastDFS and Nginx integration to achieve code analysis

FastDFS & Nginx Integration: The tracker is c...

js to realize web music player

This article shares simple HTML and music player ...

An article teaches you how to use Vue's watch listener

Table of contents Listener watch Format Set up th...

Div nested html without iframe

Recently, when doing homework, I needed to nest a ...

List rendering instructions for efficient development of Vue front-end

v-for directive Speaking of lists, we have to men...

Problems with using multiple single quotes and triple quotes in MySQL concat

When dynamically concatenating strings, we often ...

Reasons and methods for Waiting for table metadata lock in MySQL

When MySQL performs DDL operations such as alter ...

What you need to know about msyql transaction isolation

What is a transaction? A transaction is a logical...