Bash 基础知识系列 1:创建并运行你的第一个 Bash Shell 脚本

888u

Last update at :2024-04-23,Edit by888u

Translated and adapted from: https://itsfoss.com/create-bash-script/


This is the beginning of a new tutorial series . In this article, you will become familiar with bash scripting.

This series assumes that you are familiar with the Linux terminal. You don’t have to be a master, but it’s good to know the basics. It doesn’t matter if you are not familiar with it. We will plagiarize and update the series of articles later.


Who is this series suitable for?

Anyone who wants to start learning Bash Shell scripting.

If you are a student taking shell scripting as part of your course, this series is for you.

If you are a regular desktop Linux user, this series will help you understand most of the shell scripts you encounter while exploring various software and fixes. You can also use it to automate some common repetitive tasks.

If it is purely to play VPS more smoothly, this series of tutorials is also suitable for you.

By the end of this Bash Basics series, you should be able to write simple to intermediate Bash scripts.

All chapters in this series have sample exercises so you can learn by doing.

Here you will learn Bash Shell scripting. Although there are other shells with essentially the same syntax, their behavior still differs in some ways. Bash is the most common and versatile Shell, so start learning Shell scripts with Bash.


Your first shell script: Hello World!

Open a terminal. Now create a new directory to hold all the scripts you will create in this series:

mkdir bash_scripts

Now switch to this Newly created directory:

cd bash_scripts

Let's create a new file here:

touch hello_world.sh

Now, edit the file and add a line echo Hello World to it. You can do this using the append mode of the cat command (using >):

cat > hello_world.sh
echo Hello World
^C

I prefer adding new lines when adding text using the cat command.

Press Ctrl+C or Ctrl+D to exit the append mode of the cat command. Now, if you look at the contents of the script hellow_world.sh, you should only see one line.

The critical moment has come. You have created your first shell script. It's time to run the shell script.

Do this:

bash hello_world.sh

The echo command simply displays whatever is given to it . In this case, the shell script should print "Hello World" to the screen.

Congratulations! You just successfully ran your first shell script. How cool!

The following is a replay of all the above commands for your reference.


Another way to run a shell script

Most of the time, You will run the shell script this way:

./hello_world.sh

This will generate an error because as the script The file does not yet have execution permission.

bash: ./hello_world.sh: Permission denied

Add execution permissions to the script:

chmod u+x hello-world.sh

Now, you can run it like this:

./hello_world.sh

So, you learned two ways to run a shell script. It's time to turn our attention back to Bash.


Turn your Shell script into a Bash script

Confused? There are actually several shells available in Linux. Bash, Ksh, Csh, Zsh and more. Among them, Bash is the most popular and is installed by default in almost all distributions.

Shell is an interpreter. It accepts and runs Linux commands. Although the syntax of most shells remains the same, their behavior may differ at some points. For example, the processing of brackets in conditional logic.

This is why it is important to tell the system which shell to use to interpret the script.

When you use bash hello_world.sh, you are explicitly using the Bash interpreter.

But when you run the shell script this way:

./hello_world.sh

The system will use whatever shell you are currently using to run the script.

To avoid unnecessary surprises due to different syntax treatments, you should explicitly tell the system which shell script it is.

How to do it? Use release partner (#!). Typically, # is used for comments in shell scripts. However, if #! is used as the first line of a program, its special purpose is to tell the system which shell to use.

So, change the contents of hello_world.sh so that it looks like this:

#!/bin/bash
echo Hello World

Now, you can run the shell script as usual, knowing that the system will use the Bash Shell to run the script.

If you find it inconvenient to edit script files in the terminal, as a desktop Linux user, you can use Gedit or other GUI text editor to write the script and run it in the terminal.


Practice Time

It’s time to practice what you’ve learned. Here are some basic exercises for this level:

Write a Bash script that prints "Hello Everyone"

Write a Bash script that displays the current working directory (hint: use the pwd command)< /p>

Write a shell script that prints your username using: "My name is XYZ" (hint: use $USER)


The answer can be discussed in this dedicated thread in the community forum: https://itsfoss.community/t/practice-exercise-in-bash-basics-series-1-create-and-run-your-first-bash- shell-script/10682

The last exercise uses $USER. This is a special variable that prints the username.

This brings us to the topic of the next chapter in the Bash Basics series: variables.

Please continue to pay attention to the content below.


Recommended site search: website server hardware configuration, anti-complaint server, independent IP host, rent a server, shopex virtual host, Guangzhou website registration, apply for free space and Domain name virtual host application, Hong Kong server, IP rental,

Bash 基础知识系列 1:创建并运行你的第一个 Bash Shell 脚本

All copyrights belong to 888u unless special state
取消
微信二维码
微信二维码
支付宝二维码