LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-12-2021, 02:42 AM   #16
ychaouche
Member
 
Registered: Mar 2017
Distribution: Mint, Debian, Q4OS, Mageia, KDE Neon
Posts: 160

Original Poster
Rep: Reputation: 8

I agree ondoho. It's a matter of doing things right.
 
Old 09-13-2021, 09:09 AM   #17
Chuck56
Member
 
Registered: Dec 2006
Location: Colorado, USA
Distribution: Slackware
Posts: 775

Rep: Reputation: 324Reputation: 324Reputation: 324Reputation: 324
Quote:
Originally Posted by ychaouche View Post
Dear LQ,

What is the "standard" way of programming in bash ? can my functions simply
change global variables inside their code and the rest of my script expects
global variables to be changed anytime, or do you write functions that take
parameters and return values ?

It is easy to receive or return a number or a string, but I still have to figure out how to receive/return multiple values.
Multiple values can be returned using multiple variables, indexed arrays, associative arrays, delimited text, the possibilities go on...

As others have pointed out, there is no "standard" for bash which is why you find no end to suggested approaches. Functions are a great way to modularize your bash scripts and over time you will find a way to build a library of reusable functions.

I like the "write everything as a function" approach when writing bash scripts. This promotes modularization and helps control variable scope when variables are declared local to functions. I prefer a function return a text response instead of changing a variable but both have valid uses.

If you are writing scripts just for yourself then experiment and find what makes sense to you. It's all about ease of maintainability. If you look at a script a year later and can't figure out how you got it to work then maybe that's too complicated a method. Over time you'll gravitate to your preferred methods.

Have fun & enjoy the ride!
 
1 members found this post helpful.
Old 09-13-2021, 11:13 AM   #18
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,854

Rep: Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692
Quote:
Originally Posted by Chuck56 View Post
I prefer a function return a text response instead of changing a variable but both have valid uses.
returning text requires subshell which is (in most cases) extremely expensive (like 1000 times slower, requires 1000 times more RAM...) - comparing to a simple function returning an exit code and setting a variable.
But sometimes people don't care (even if they have no resources to waste).
 
Old 09-13-2021, 11:35 AM   #19
Chuck56
Member
 
Registered: Dec 2006
Location: Colorado, USA
Distribution: Slackware
Posts: 775

Rep: Reputation: 324Reputation: 324Reputation: 324Reputation: 324
Quote:
Originally Posted by pan64 View Post
returning text requires subshell which is (in most cases) extremely expensive (like 1000 times slower, requires 1000 times more RAM...) - comparing to a simple function returning an exit code and setting a variable.
But sometimes people don't care (even if they have no resources to waste).
True if you're running a low resource system or a high utilization system. In my case all the systems I maintain (physical/virtual/containers) have more than enough resources.

Both methods have their place. The method that produces the easiest to maintain script is key for me.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH: Global Variables carolflb Linux - Newbie 4 01-23-2010 07:55 AM
LXer: OpenClinica Global Conference to Bring Together Global Community for Open Sourc LXer Syndicated Linux News 0 01-08-2010 10:50 AM
How to check the cpu utilization on all non global zones from Global Zone rajaniyer123 Solaris / OpenSolaris 3 10-09-2008 01:43 AM
Bash: How to setup "global" environment variables? ArthurHuang Programming 2 02-07-2008 12:24 PM
How to share a ZFS file system between a global zone and a non global zone? crisostomo_enrico Solaris / OpenSolaris 7 11-28-2007 08:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration