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 shell uses functions

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how shell uses functions. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Function

Variables defined anywhere are global variables. If you want to define local variables, you need to add the local keyword

Functions in shell can also use recursion

#! / bin/bash function factorial {if [[$1-eq 1]]; then echo 1 else local temp=$ [$1-1] local result= `factorial $temp`echo $[$result * $1] fi} result= `factorial 5` echo $result

Create a function library

Set a function in another file, and then load it into the current file through the source command

Use functions in the exercise of commands

Define the function in ~ / .bashrc

Pass an array to a function

#! / bin/bash # adding values in an array function addarray {local sum=0 local newarray newarray= (`newarray "$@" `) for value in ${newarray [*]} do sum=$ [$sum+$value] done echo $sum} myarray= (12345) echo "The original array is: ${myarray [*]}" arg1= `echo ${myarray [*]} `result= `array $arg1`echo "The result is $result" Thank you for reading! This is the end of this article on "how to use functions in shell". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

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

Development

Wechat

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

12
Report