• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar

The Geek Diary

HowTos | Basics | Concepts

  • Solaris
    • Solaris 11
    • SVM
    • ZFS
    • Zones
    • LDOMs
    • Hardware
  • Linux
    • CentOS/RHEL 7
    • RHCSA notes
    • SuSE Linux Enterprise
    • Linux Services
  • VCS
    • VxVM
  • Interview Questions
  • oracle
    • ASM
    • mysql
    • RAC
    • oracle 12c
    • Data Guard
  • DevOps
    • Docker
    • Shell Scripting
  • Hadoop
    • Hortonworks HDP
      • HDPCA
    • Cloudera
      • CCA 131

How to use command line shell functions in Linux

By admin

Functions, a powerful feature of shell programming, is a group of commands organized by common functionality. These easy-to-manage units, when called return a single value, and do not output anything. Using a function involves two steps:
1. Defining the function
2. Invoking the function

Shell function Vs shell alias

Shell functions and aliases are different on two counts.
– aliases do not take arguments as functions do.
– if a command name is defined as a function and an alias, the alias takes precedence.

Display shell functions

To display the function defines, use the following command:

# typeset -f
list () 
{ 
    ls --color=auto -al | wc -l
}
num () 
{ 
    who | wc -l
}

Defining a Function

A function is defined by using the following general format:

# function [function name] { command; . . . command; }
Note: A space must appear after the opening brace and before the closing brace.

The following example defines a function called num that displays the total number of users currently logged in to the system. The num function runs the who command, whose output is further directed to the wc command.

$ function num { who | wc -l; }

Shell functions in shell scripts

Functions are not only useful in shell scripts but are also used in command-line situations where an alias is unusable. For demonstration, shell functions are run on the command line to illustrate how the functions perform.

The following example creates a function called list that displays the total number of subdirectories and files in the current directory. The list function calls the ls command, whose output is directed to the wc command:

$ function list { ls -al | wc -l; }
$ list
34

Invoking a Function

You can invoke a function by merely entering the function name on the command line or within the shell script.

$ [function name]

For example, to invoke the function num on command line, use the command below.

$ num

Filed Under: DevOps, Linux, Shell Scripting

Some more articles you might also be interested in …

  1. How to use FTP under Linux to transfer files
  2. CentOS / RHEL 6 : How to disable IPv6
  3. How to send Audit Logs to Remote Rsyslog Server in CentOS/RHEL 6,7
  4. Linux / UNIX : How to create extended partition using fdisk
  5. How to change the order of the authentication methods of ssh in CentOS/RHEL
  6. How to mount an iso file in Linux
  7. CentOS / RHEL : How to restrict SSH login by time of day
  8. How to disable write access to USB devices using “hdparm” tool
  9. Unable To Remove Files From Directory with Error “Argument list too long”
  10. Sample /etc/multipath.conf file

You May Also Like

Primary Sidebar

Recent Posts

  • Linux OS Service ‘cups’
  • “Warning: RPMDB altered outside of yum.” – On installing/updating/erasing a package using YUM command
  • How to Generate Unique IDs For MysQL Cluster Backups
  • Oracle 11g – New ASM features
  • ASM Fast Mirror Resync Feature – Example To Simulate Transient Disk Failure And Restore Disk
  • Archives
  • Contact Us
  • Copyright

© 2019 · The Geek Diary