Get the App
SLTechnology News&Howtos  ›  Servers  › 

How Linux uses functions in Shell scripts

Shulou Source: shulou.com Published: 2022-06-01 17:58:32 10月03日 Update

This article will explain in detail how Linux uses functions in Shell scripts. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Linux uses function examples in Shell scripts to explain in detail

The function of Shell

The Shell program also supports functions. A function can perform a specific function and can be called repeatedly.

The function format is as follows:

Function name () {function body}

Function call method:

Function name argument list

Example: write a function add to find the sum of two numbers, the two numbers are passed in by position parameters, and finally output the result.

Root@ubuntu:/home/study# vi Test3 The sum is Binder Bash add () {aplink 1; breadbasket 2; z = `expr $a + $b`; echo "The sum is $z" } add $1 $2 root@ubuntu:/home/study# chmod + x test3root@ubuntu:/home/study#. / test3 12 this is the end of the article on "how Linux uses functions in Shell scripts". I hope the above can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Functions scripts articles two parameters examples more good practical location content functions articles methods formats knowledge programs results numerical uses references Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Linux Xiaomi Redmi Docker