Functions in Bash

#!/bin/bash
# Basic function
print_something () {
echo Hello I am a function
}
print_something
print_something

Passing Arguments

#!/bin/bash
# Passing arguments to a function
print_something () {
echo Hello $1
}
print_something Mars
print_something Jupiter

Return Values

#!/bin/bash
# Setting a return status for a function
print_something () {
echo Hello $1
return 5
}
print_something Mars
print_something Jupiter
echo The previous function has a return value of $?

Passing Arguments

#!/bin/bash
# Passing arguments to a function
print_something () {
echo Hello $1
}
print_something Mars
print_something Jupiter

Return Values

#!/bin/bash
# Setting a return status for a function
print_something () {
echo Hello $1
return 5
}
print_something Mars
print_something Jupiter
echo The previous function has a return value of $?

the variable $? contains the return status of the previously run command or function.

References
https://ryanstutorials.net/bash-scripting-tutorial/bash-functions.php

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.