To return a string from the function in Bash, use the following methods:
- Using “echo” Command:
my_function() { echo "my string";}
- Using “printf” Command:
my_function() { printf "my string\n";}
- Using Global Variable:
my_function() { global_var="my string";}
- Using Here-document:
my_function() { cat << EOF multiline string EOF;}
In bash, functions do not return strings directly, the echo command or printf command can be employed to return the string from the function in Bash. Moreover, you can declare a global variable for returning the string from the Bash function. To get the multiline string from the function in Bash, you need to include the here-document inside the function. In this article, I am going to show you 4 methods to return a string from the bash function. Read on.
1. Using “echo” Command
Leveraging the echo command provides different ways to capture the string output of the function in Bash. The command substitution method or process substitution method can be incorporated with this command to return a string from the bash function. Here are 3 cases of it:
1.1 Using “echo” Command Alone
The easiest way to get a string from a bash function is to use only the echo
command within the function. The echo command will print the output when the function is called. Here’s how:
#!/bin/bash
# Define a function named get_string
function get_string() {
echo "Hello, World!"
}
# Call the get_string function
get_string
Here, a function is defined named get_string
. After calling the function, it returns the string “Hello, World!” using the echo
command.
The script returns the string “Hello, World!” using the “echo” command.
1.2 Using “echo” Command With Command Substitution
To return a string from the bash function, use the command substitution method with the echo
command. The command substitution takes the function output echoing by the “echo” command and assigns it directly to a variable using the $(...)
syntax. Here is the full bash script to do this:
#!/bin/bash
# Define a function named get_user
function get_user {
echo $USER
}
# Call the get_user function and capture the result in the variable "username"
username=$(get_user)
# Print a message using the value stored in the "username" variable
echo "Hello, $username!"
This script defines a function get_user
that echoes the current user’s username using the $USER
environment variable. Then, it assigns the result of calling this function to the variable “username” using command substitution $(get_user)
. Finally, echo "Hello, $username!"
prints a greeting message using the value stored in the “username” variable.
As you can see, the script returns a string containing the current user’s username.
1.3 Using “echo” Command With Process Substitution
The process substitution method encapsulates the function’s output (string of the echo
command) using $(< <(...))
syntax. It allows to use the the output of the function as a file or input stream. This method is advantageous for advanced string manipulation. Now, copy the script to return a string from the function using process substitution:
#!/bin/bash
function get_result() {
local string="Hello, I am using Ubuntu."
echo "$string"
}
result=$(< <(get_result))
echo "$result"
A function named get_result
is defined. Inside the function, a local variable named “string” is assigned with the value “Hello, I am using Ubuntu.”. The echo "$string"
then outputs the content of the variable.
result=$(< <(get_result))
captures the output of the get_result
function. echo "$result"
prints the value of the “result” variable.
You can notice that the function returns the string “Hello, I am using Ubuntu.”.
2. Using “printf” Command
The printf command provides precise control over the output of the function. To return intricate and dynamic strings from a Bash function, using this method is a great idea. Now, check the bash script to see how the function returns a string with the printf
command:
#!/bin/bash
function return_string {
printf "Hello, I am using Linux\n"
}
# Call the return_string function
return_string
In this script, the return_string
function uses printf
command to return a string “Hello, I am using Linux” once the function is called. The \n
at the end of the line adds a newline character, separating the script’s output from the command prompt.
You can see that the function returns the string “Hello, I am using Linux”.
3. Using Global Variable
A global variable is accessible from inside or outside of a function. Use such a variable within a function to return any string from the function. Here’s the entire bash script to get a clear insight:
#!/bin/bash
# Define a global variable
string_global() {
string="Hello, I am using Ubuntu."
}
string_global
echo $string
First, a global variable string="Hello, I am using Ubuntu."
is defined within the function string_global
. After calling the function, it prints the value of the global variable.
The function returns a string “Hello, I am using Ubuntu.” using a global variable.
4. Using Here-document
To return multiline strings from a function, one of the convenient ways is to use the cat
command with a here document. The here document can craft a string block without manually escaping special characters. Here is how to return a multiline string from a function:
#!/bin/bash
function get_message {
cat <<EOF
Hello, welcome to Linuxsimply.
I am using Ubuntu.
EOF
}
get_message
Here, the get_message
function uses a here document to define a multiline string. The cat command concatenates (or displays) the lines within the here document.
The string content is written between <<EOF
and EOF
. Finally, get_message
calls the function and prints the multiline string.
This script returns the multiline string from the function using here document.
Practice Tasks on Returning String From Bash Function
Elevate your Bash scripting abilities by practicing the following tasks:
- Define a function that uses a global variable to return “hello Linux” from the bash function.
- Develop a function that checks if a string is empty and returns an appropriate message.
- Write a Bash script that defines a function string_length that takes a string as a parameter and returns its length.
- Generate a function that takes a string as a parameter and returns its uppercase version in the output.
- Develop a function that takes a string and returns its reversed form in the output.
Conclusion
I hope that this article has helped you understand the ways to get a string from a function in bash. In this article, I have shown 4 detailed methods to get a string from a function. You can choose the method of your choice. Happy scripting!
People Also Ask
How can I return string from a function in Bash?
To return a string from a function in Bash, you can apply the echo
command within the function alone or with the command substitution method or process substitution method. Also, you can define a global variable or here-document in the function to accomplish this task.
How do you return an empty string in Bash?
In Bash, you can return an empty string from a function by simply not echoing any content within the function. Here’s an example bash script:
#!/bin/bash
get_empty_string() {
# This function returns an empty string
}
result=$(get_empty_string)
echo "Result: '$result'"
Can a string have numbers?
Yes, a string can have numbers. In the programming language, a string is usually a collection of characters. These characters can be letters, numbers, symbols, or a combination of them.
How do I extract a string from a line in Bash?
To extract a string from a line in Bash, you can use the awk
command. Suppose, you want to extract the “Linux” word from a line “Hello Linux Users.”. To do so, apply word=$(echo "$line" | awk '{print $2}')
in the Bash script. Here, the awk command uses the $2
parameter to extract the 2nd word from the line.
Related Articles
- How to Exit From Function in Bash? [5 Ways]
- How to Return Exit status “0” in Bash [6 Cases]
- How to Return Boolean From Bash Function [6 Methods]
- How to Return Array From Bash Function? [7 Methods]
<< Go Back to Return Values From Bash Function | Bash Functions | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners