Bash: Return a string from bash function

bash, linux

Solution

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

- Returning Values from Bash Functions

- How to return a string value from a Bash function

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 fileName

name1=$(makeName "type1")
name2=$(makeName "type2")

echo $name1
echo $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 fileName

makeName "type1" ; name1=${__}
makeName "type2" ; name2=${__}

echo $name1
echo $name2
$ ./sample.sh 
Enter fileName:filename
Enter Ext: ext1
Enter Ext: ext2
filename.type1.ext1.log
filename.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 fileName

makeName "type1" $fileName theResult ; name1=${theResult}
makeName "type2" $fileName theResult ; name2=${theResult}
echo $myresult

echo $name1
echo $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=$3

myresult="$__fileName.$__type.$ext.log"
...

Problem

I need a bash function to return a dynamically constructed string to returned to the caller space. i.e, ``` makeName() { echo "Enter Ext: " read ext return "$fileName.$1.$ext.log" } echo -n "Enter fileName:" read fileName name1=makeName "type1" name2=makfName "type2" ``` So I can get two different filenames with same base file name. I tries doing like this, ``` # echo $(makeName "type1") ``` But this code is struck somewhere with out any reason or error. I expect it to accept some kind of I/O with that proc also. That is not happening.

Original source

Related problems