I currently have a file called function.sh that contains a function for Bash:
function dimeNombre {
nombre=$LOGNAME
echo -n $nombre
}
And another file called hello.sh with the following content:
#!/bin/bash
source "/home/pdelgado/Escritorio/funcion.sh"
dimeNombre "$?"
echo "Hola $1"
I need the hello.sh script to call funcion.sh and for it to return the username so that hello.sh uses that parameter in the echo.
I have a huge mess with this using external functions and how to pass parameters from one side to another.
Thanks.
In Bash there is not a
return
to use as we know it in other languages. Since the underlying idea is that the output of one command is used for the next , what we do is tell it what's output. Hence in functions we return things with an "echo" or "printf" or whatever it returns to stdout.Therefore, to call
dimeNombre
and collect its result, you must execute the command and store it in a variable:Another example:
We define the function
dime_nombre
:We call you:
We call it and store it in a variable: