Bash: Return a string from bash function [duplicate]
The return
statement used by bash
is used to return a numeric value as a status code to be retrieved through $?
by the calling function. You can not return a string. See also
You can either use a special global variable as proposed by @konsolebox, or echo
the return value inside your function, and use command substitution when calling the function:
makeName(){ echo "$fileName.$1.log"}echo -n "Enter fileName:"read fileNamename1=$(makeName "type1")name2=$(makeName "type2")echo $name1echo $name2
[UPDATE]
The updated question shows that you intend to read yet another value inside the makeName
function, while the function also intends to echo
some prompt for the user. So the command substitution approach will not work in that case - you need to use a global variable, like
makeName() { echo -n "Enter Ext: " read ext __="$fileName.$1.$ext.log"}echo -n "Enter fileName:"read fileNamemakeName "type1" ; name1=${__}makeName "type2" ; name2=${__}echo $name1echo $name2
$ ./sample.sh Enter fileName:filenameEnter Ext: ext1Enter Ext: ext2filename.type1.ext1.logfilename.type2.ext2.log
Yet better, for cleaner code and to avoid using global variables inside your function, you could use the approach described at Returning Values from Bash Functions and pass the name of the return variable as parameter, and ideally also pass the fileName
as parameter:
makeName() { local __type=$1 local __fileName=$2 local __resultvar=$3 local ext local myresult echo -n "Enter Ext: " read ext myresult="$__fileName.$__type.$ext.log" eval $__resultvar="'$myresult'"}echo -n "Enter fileName:"read fileNamemakeName "type1" $fileName theResult ; name1=${theResult}makeName "type2" $fileName theResult ; name2=${theResult}echo $myresultecho $name1echo $name2
Side Note: See Why should eval be avoided in Bash, and what should I use instead? for a discussion why eval
should be avoided. When using bash version 3.1 or higher, you can use printf
instead of eval
:
...printf -v "$__resultvar" '%s' "$myresult"...
And, finally, with bash 4.3 or higher, we can assign the nameref attribute to a variable using declare -n
, so that the variable is effectively a reference to another variable:
...declare -n myresult=$3myresult="$__fileName.$__type.$ext.log"...
Better way is to pick a general variable you could always use. Example:
makeName() { __="$fileName.$1.log"} echo -n "Enter fileName:"read fileNamemakeName "type1"name1=$__makeName "type2"name2=$__echo "$name1"echo "$name2"
Summoning subshells just to get a value from a function is really inefficient and slow.