Computer >> Computer tutorials >  >> Programming >> BASH programming

Bash Shell Functions Tutorial with 6 Practical Examples

Bash Shell Functions Tutorial with 6 Practical ExamplesBash shell functions are a way to group several UNIX / Linux commands for later execution using a single name for the group. Bash shell function can be executed just like a regular Unix command. Shell functions are executed in the current shell context without creating any new process to interpret them.

Both bash aliases and functions allow you to define shortcuts for longer or more complicated commands. However, aliases don’t allow control-flows, arguments, and other trickery things these functions will allow as explained in this tutorial.

This article is part of the on-going bash tutorial series. Refer to our earlier tutorial about bash introduction, bash exist status and bash alias examples.

Syntax to create a bash function:

function functionname()
{
commands
.
.
}
  • function is a keyword which is optional.
  • functionname is the name of the function
  • commands – List of commands to be executed in the functions.

Function accepts arguments. During execution, the arguments to the function becomes the positional parameters. Positional parameter 0 will have the scriptname which remains unchanged.

You can call the bash function from the command line as shown below:

$ functionname arg1 arg2
  • When shell interprets a Linux command, it first looks into the special built-in functions like break, continue, eval, exec etc., then it looks for shell functions.
  • The exit status of the bash function is the exit status of the last command executed in the function body.

Note: Place the shell function definitions in a shell start up file (for example, .bash_profile ). This way, the shell function is always available for you from the command line. Refer to our earlier bash execution sequence article to identify when .bash_profile will get executed.

Example 1: Function to display long list of files with the given extension

The function “lsext” is used to find the list of files in the current directory, which has the given extension as shown below. This function uses the combination of find command and ls command to get the job done.

$ function lsext()
{
find . -type f -iname '*.'${1}'' -exec ls -l {} \; ;
}

$ cd ~

$ lsext txt
-rw-r--r-- 1 root root   24 Dec 15 14:00 InMorning.txt
-rw-r--r-- 1 root root  184 Dec 16 11:45 Changes16.txt
-rw-r--r-- 1 root root  458 Dec 18 11:04 Changes18.txt
-rw-r--r-- 1 root root 1821 Feb  4 15:01 ChangesOfDB.txt

Example 2. Bash Function to execute a given Linux command on a group of files

In the following example, function “batchexec” finds the files with the given extension and executes the given command on those selected files.

$ function batchexec()
{
find . -type f -iname '*.'${1}'' -exec ${@:2}  {} \; ;
}

$ cd ~

$ batchexec sh ls

$ batchexec sh chmod 755

$ ls -l *.sh
-rwxr-xr-x 1 root root  144 Mar  9 14:39 debug.sh
-rwxr-xr-x 1 root root 5431 Jan 25 11:32 get_opc_vers.sh
-rwxr-xr-x 1 root root   22 Mar 18 08:32 t.sh

In the above example, it finds all the shell script files with the .sh extension, and changes its permission to 755. (All permission for user, for group and others read and execute permission). In the function definition you could notice “${@:2}” which gives the second and following positional parameters (shell expansion feature).

Example 3. Bash Function to generate random password

The following function is used to generate the random strong passwords with special character for the given length. If length is not given by default it generates with 12 characters length.

$ function rpass() {
        cat /dev/urandom | tr -cd '[:graph:]' | head -c ${1:-12}
}

$ rpass 6
-Ju.T[[

$ rpass
Gz1f!aKN^""k

In the above example, when rpass is executed with the argument 6, it generates random password with 6 characters and rpass without argument generates 12 character length password. ${1:-12} means if $1 is unset or null 12 will be returned else value of $1 will be substituted.

Example 4. Bash function to get IP address of a given interface

The following example defines a function called ‘getip’ which accepts interface name as an argument, and gives the IP address assigned on the given interface in the machine. ( by default it returns eth0 ip address ). This uses the ifconfig command to get the ip-address.

$ function getip()
{
/sbin/ifconfig ${1:-eth0} | awk '/inet addr/ {print $2}' | awk -F: '{print $2}';
}

$ getip
15.110.106.86

$ getip eth0
15.110.106.86

$ getip lo
127.0.0.1

Example 5. Bash function to print the machines details

This example defines the function which gives all the required information about the machine. Users can define and call this function in the start up files, so that you will get these information during startup.

$ function mach()
{
    echo -e "\nMachine information:" ; uname -a
    echo -e "\nUsers logged on:" ; w -h
    echo -e "\nCurrent date :" ; date
    echo -e "\nMachine status :" ; uptime
    echo -e "\nMemory status :" ; free
    echo -e "\nFilesystem status :"; df -h
}

$ mach
Machine information:
Linux dev-db 2.6.18-128.el5 #1 SMP Wed Dec 17 11:41:38 EST 2008 x86_64 GNU/Linux

Users logged on:
root     pts/2    ptal.mot Wed10    0.00s  1.35s  0.01s w -h

Current date :
Thu Mar 18 11:59:36 CET 2010

Machine status :
 11:59:36 up 7 days, 3 min,  1 user,  load average: 0.01, 0.15, 0.15

Memory status :
             total       used       free     shared    buffers     cached
Mem:       2059768    2033212      26556          0      81912     797560
-/+ buffers/cache:    1153740     906028
Swap:      4192956      48164    4144792

Filesystem status :
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              12G   12G     0 100% /
tmpfs                1006M  377M  629M  38% /dev/shm
/dev/sdc5             9.9G  409M  9.0G   5% /mydisk

Example 6: Bash function to format ls output better

The following function will clear the screen, place the cursor at the top of the screen, executes ls, and then places the cursor at the end of the screen.

$ function ll ()
{
    clear;
    tput cup 0 0;
    ls --color=auto -F --color=always -lhFrt;
    tput cup 40 0;
}

$ ll

Display the function code using type command

type is a shell built-in used to view the function code.

Syntax:
type function-name
$ type ll
ll is a function
ll ()
{
    clear;
    tput cup 0 0;
    ls --color=auto -F --color=always -lhFrt;
    tput cup 40 0;
    alias ls="ls --color=auto -F"
}

For your easy reference, get all the 6 functions mentioned this article from this sample .bash_profile functions file.

Add all these functions to your ~/.bash_profile file, to make sure you have access to these functions all the times without having to create it every time.