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

Linux's 10 tips for writing Bash scripts

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Shell scripting is the easiest way for you to learn or practice programming under Linux. In particular, it is a necessary skill for system administrators to deal with automation tasks and to develop new and simple utilities or tools (to name just a few).

In this article, we will share 10 practical tips for writing efficient and reliable bash scripts, including:

1. Write more comments in the script

This is a recommended practice that can be used not only in shell scripting programs, but also in all other types of programming. Commenting on a script can help you or others to understand what different parts of the script are doing when flipping through your script.

For beginners, comments are defined by the # sign.

# TecMint is the best site for browsing all kinds of Linux articles

2. Exit the script when the run fails

Sometimes even if some commands fail, bash may continue to execute the script, which affects the rest of the script (which eventually leads to logic errors). Exit script execution in the event of a command failure using the following line:

10 tips for writing Bash scripts efficiently in Linux

3. Exit the script when Bash uses undeclared variables

Bash may also use undeclared variables that can cause logic errors. So tell bash to exit script execution when it tries to use an undeclared variable:

10 tips for writing Bash scripts efficiently in Linux

4. Use double quotation marks to reference variables

Using double quotation marks when referencing (using the value of a variable) helps prevent unnecessary matching due to the separation of words caused by spaces and the recognition and extension of wildcards.

Look at the following example:

10 tips for writing Bash scripts efficiently in Linux

Save the file and exit, and then run as follows:

$. / names.sh

10 tips for writing Bash scripts efficiently in Linux

Use double quotation marks in a script

5. Use functions in scripts

Except for very small scripts (only a few lines of code), always remember to use functions to modularize the code and make the script more readable and reusable.

The syntax for writing a function is as follows:

10 tips for writing Bash scripts efficiently in Linux

When writing a single line of code, use a termination symbol after each command:

Check_root () {command1; command2;}

6. Use = instead of = when comparing strings

Note that = is synonymous with =, so only a single = is used for string comparison, for example:

10 tips for writing Bash scripts efficiently in Linux

7. Replace it with $(command) instead of the old command

Command substitution replaces the command itself with the output of this command. Use $(command) instead of quotation marks command for command substitution.

This approach is also recommended by shellcheck tool, which displays warnings and recommendations for shell scripts. For example:

10 tips for writing Bash scripts efficiently in Linux

8. Declare static variables with readonly

A static variable does not change; its value cannot be modified once it is defined in the script:

10 tips for writing Bash scripts efficiently in Linux

9. Environment variables are named in uppercase letters, while custom variables are named in lowercase

All bash environment variables are named in uppercase letters, so name your custom variables in lowercase letters to avoid variable name conflicts:

10 tips for writing Bash scripts efficiently in Linux

10. Always debug long scripts

If you are writing a bash script with thousands of lines of code, troubleshooting can turn into a nightmare. To make it easy to fix some errors before the script is executed, you need to do some debugging. A guide will be updated later to help you master these skills:

How to enable Shell script debugging mode in Linux

How to perform syntax checking debug mode in a Shell script

How to track the execution of debug commands in Shell scripts

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

Servers

Wechat

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

12
Report