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 write functions in Bash

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article is about how to write functions in Bash. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

Reduce redundancy and maintenance in code by writing functions.

When programming, you are actually defining a procedure or routine to be executed by the computer. A simple analogy is to compare computer programming to baking bread: once you list the ingredients to set up your work environment, then you list the steps you must take to bake bread. In programming and baking, certain steps must be repeated at different intervals. For example, in toast, this may be the process of yeast culture:

STIR=100SNOOZE=86400 function feed_culture { remove_from(pantry) add(flour, water) stir($STIR) sleep($SNOOZE)}

Then, knead and wake up the dough:

KNEAD=600SNOOZE=7200 function process_dough { remove_from(proofing_drawer) knead($KNEAD) return_to_drawer($SNOOZE)}

In programming, these subroutine routines can be expressed as functions. Functions are important to programmers because they help reduce redundancy in code, thereby reducing the amount of maintenance required. For example, in a hypothetical scenario where bread is baked programmatically, if you need to change the dough proofing time, you only need to change the time once if you used the function before, either using the variable (SNOOZE in the example code) or directly in the subroutine where the dough is processed. This saves you a lot of time because you don't have to go through your code base for every dough that might be rising, let alone worry about missing one. Many bugs are caused by missing values that are not changed or by executing incorrect sed commands, hoping to catch all the possibilities without having to look for them manually.

In Bash, defining functions is as simple as using them, whether in a script or in a separate file. If you save the function to a separate file. Then you can source it into a script, just as you can include libraries in C or C++ or import modules into Python. To create a Bash function, use the keyword function:

function foo {# code here}

Here's an example of how to use parameters in a function (some people design it for you, so it might be simpler):

#!/ usr/bin/env bashARG=$1 function mimic { if [[ -z $ARG ]]; then ARG='world' fi echo "hello $ARG"} mimic $ARG

The results were as follows:

$ ./ mimichello world$ ./ mimic everybodyhello everybody

Notice the last line of the script, which executes the function. This is a common confusion point for novices to scripting: functions don't execute automatically. They exist as latent routines until called.

If the function is not called, the function is simply defined and never runs.

If you are new to Bash, try executing the sample script once with the last line included, and then again with the last line commented out.

using the function

Functions are important programming concepts, even for simple scripts. The more comfortable you are with functions, the easier it will be for you to deal with a complex problem that requires more dynamics than just declarative command-line behavior. Keeping generic functions in separate files also saves you some work, as it will help you build commonly used programs so that you can reuse them across projects.

Thank you for reading! About "how to write functions in Bash" this article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

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