Using a function in bash allows you to create something in Linux that works as if it were a script within a script. Whenever the data being processed matches a set of conditions, your script can call a function that does further processing. The format of a function is very straightforward. The syntax looks like this: () { } You can also use the following format that uses the word “function” if you prefer: function { } In fact, you can also create a function on a single line if the commands to be run are limited, but note the required “;” that follows the command(s): () { ; } Replace the elements inside the and > signs (along with the signs as well) with the name you want to use and the commands you want to run. Functions can include any variety and any number of commands. In the simple example below, one function (replspaces) is defined that will replace the spaces in text with underscores using a sed command. A second (dropspaces) was added that removes the spaces. #!/bin/bash replspaces () { echo $@ | sed 's/ /_/g' } dropspaces () { echo $@ | sed 's/ //g'; } replspaces "Hello World" replspaces `date` dropspaces "Hello World" dropspaces `date` When you run the script, you should see something like this: $ nospaces Hello_World Tue_Oct_12_12:55:26_AM_EDT_2022 HelloWorld TueOct1212:55:26AMEDT2022 In the somewhat more complex example below, the script creates a list of UIDs from the /etc/passwd file and then sends any that are in the UID range for user accounts (1000 and higher) to the function to gather the group memberships for each of these accounts. It uses the continue command to avoid processing the nobody account. Notice that the function needs to be defined before it is called. #!/bin/bash # obtain additional group memberships from the /etc/group file listgroups () { echo -n "$uid: " uname=`grep ":$uid:" /etc/passwd | awk -F: '{print $1}'` echo $uname for group in `grep $uname /etc/group | awk -F: '{print $1}'` do if [ $group != $uname ]; then echo " $group" fi done } # gather list of UIDs from the /etc/passwd file while read -r line do uids=`awk -F: '{print $3}'` done When the script is run, it generates output like this: $ list_users_and_groups 1000: shs wheel techs 1002: bugfarm 1003: dbell 1004: dumbo 1005: eel wheel As you can see, users such as “dbell” have only one account. Group names from the /etc/group file will only be added if they are different than the account’s username. Wrap-up Functions can be used for selective processing any time the need arises. They generally make scripts easier to read by separating a group of commands that focus on a single task, but can also contain code that would otherwise need to be included numerous times in a script. Related content how-to How to examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe