Hi I need to get the output (generated via echo) from a function inside a RPM spec file.

The function is

get_server_ip() {
        some staff
        echo $nodeip
        return 0

It is located in the file that I source:

source /usr/local/lib/script.sh

How can I get the value from the function inside script and use it as argument for ssh-keygen? I use GNU Bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu).


1 Answer 1


You can use command substitution to store the output printed by a command or function to stdout into a shell variable: Using


in your script will store the string printed by get_server_ip() into the variable my_value, which you can then reference as usual using "$my_value" in the further script code.

If you are running this from an RPM spec file, be sure to define bash as a requirement using the appropriate Requires directive (see here for example).

  • will the ssh-keygen some stuff -C name@$get_server_ip work? Dec 15, 2020 at 14:13
  • It should work. However, be sure to write it correctly as $( ... ), and double-quote it as in "$( ... )" to prevent unwanted word splitting (which should not be an issue for IP addresses, but in general is a good idea to do).
    – AdminBee
    Dec 15, 2020 at 14:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .