InfoHeap
Tech tutorials, tips, tools and more
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Bash shell scripting

  • Bash - add a number to a variable
  • Bash - append text to a variable
  • Bash - how to check if a variable is set
  • Bash - how to compare file timestamps
  • Bash - how to find last command exit status code
  • Bash - how to get main program and current file dir location
  • Bash - how to redirect stderr to stdout or file
  • Bash - how to run custom commands at script exit
  • Bash - how to stop at error
  • Bash - how to use functions - quick tutorial
  • Bash - iterate over array
  • Bash - local and global variables
  • Bash - newline and other escape character in string
  • Bash - pass all arguments from one script to another
  • Bash - set default value if a variable is empty
  • Bash - variables in double quotes vs without quotes
  • Bash associative array tutorial
  • Bash check if file begins with a string
  • Bash shell - check if file or directory exists
  • Can global variables be modified in bash function?
  • Find memcache request hit rate on linux command line
  • How to return a value from bash function
  • Iterate over specific file extension in a dir in shell script
  • Linux - Yesterday's Date in YYYYMMDD format
  • bash - extract urls from xml sitemap
  • bash - how to use regex in if condition
 
  • Home
  • > Tutorials
  • > Bash shell scripting

Bash – how to use functions – quick tutorial

By admin on Dec 23, 2015

Using functions in bash is fairly useful to organise and reuse code. Here is quick beginner tutorial with examples on using bash functions.

Declaring functions in bash

Functions can be declared either using function f1 () or f1 () as shown below:

#!/bin/bash
function f1 () {
  echo "in f1"
}
f2 () {
  echo "in f2"
}

f1
f2
in f1
in f2
Env: GNU bash, version 4.2.46

Passing arguments to functions in bash

Arguments are accessible using $@ or $1, $2, etc.

#!/bin/bash
function f1 () {
  echo "in f1 all args=$@"
  echo "in f1 first arg=$1"
}

f1 A B B
in f1 all args=A B B
in f1 first arg=A
Env: GNU bash, version 4.2.46

Calling functions in bash

Function f1 can directly called as f1 in as sub-shell command (e.g. $(f1)). When called with sub-shell mode it will return the outcome of echo, etc. instead of printing.

#!/bin/bash
function f1 () {
  echo "in f1"
}

## inline function mode
f1

# in sub-shell mode
ret=$(f1)
echo "ret=$ret"
in f1
ret=in f1
Env: GNU bash, version 4.2.46

Suggested posts:

  1. Can global variables be modified in bash function?
  2. Bash – local and global variables
  3. How to return a value from bash function
  4. Bash check if file begins with a string
  5. Bash – iterate over array
  6. Bash – append text to a variable
  7. Bash – pass all arguments from one script to another
  8. Bash – variables in double quotes vs without quotes
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Bash shell scripting, Devops Interview Questions, Linux Interview Questions, Tutorials

Follow InfoHeap

facebook
twitter
googleplus
  • Browse site
  • Article Topics
  • Article archives
  • Recent Articles
  • Contact Us
  • Omoney
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries

Copyright © 2023 InfoHeap.

Powered by WordPress