Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8223771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:49:52+00:00 2026-06-07T14:49:52+00:00

In my program I am trying to return a value from a function, the

  • 0

In my program I am trying to return a value from a function, the return value is string. Everything works fine(atleast some part), if I echo the value once it is returned, but it is not even calling the function, if I dont return…. Consider the code below….

#!/bin/bash

function get_last_name() {
    echo "Get Last Name"
    ipath=$1

    IFS='/'
    set $ipath
    for item
            do
                    last=$item
            done

    echo $last
}

main() {
    path='/var/lib/iscsi/ifaces/iface0'
    current=$(get_last_name "$path")
    echo -n "Current="
    echo $current
}


main

It gives me an output like this

OUTPUT
     Current=Get Last Name iface0

If I comment the echo $current, then the I am not even seeing the “Get Last Name”, which makes to come to conclusion, that it is not even calling the function. Please let me know what mistake I am making. But one thing I am sure, bash is the ugliest language I have ever seen…….

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T14:49:55+00:00Added an answer on June 7, 2026 at 2:49 pm

    Functions do not have return values in bash. When you write

    current=$(get_last_name "$path")
    

    you are not assigning a return value to current. You are capturing the standard output of get_last_name (written using the echo command) and assigning it to current. That’s why you don’t see “Get last name”; that text does not make it to the terminal, but is stored in current.


    Detailed explanation

    Let’s walk through get_last_name first (with some slight modifications to simplify the explanation):

    function get_last_name () {
        ipath=$1
    
        local IFS='/'
        set $ipath
        for item
        do
            last=$item
        done
    
        echo "Get Last Name"
        echo $last
    }
    

    I added the local command before IFS so that the change is confined to the body of get_last_name, and I moved the first echo to the end to emphasize the similarity between the two echo statements. When get_last_name is called, it processes its single argument (a string containing a file path), then echoes two strings: “Get Last Name” and the final component of the file path. If you were to run execute this function from the command line, it would appear something like this:

    $ get_last_name /foo/bar/baz
    Get Last Name
    baz
    

    The exit code of the function would be the exit code of the last command executed, in this case echo $last. This will be 0 as long as the write succeeds (which it almost certainly will).

    Now, we look at the function main, which calls get_last_name:

    main() {
        path='/var/lib/iscsi/ifaces/iface0'
        current=$(get_last_name "$path")
        echo -n "Current="
        echo $current
    }
    

    Just like with get_last_name, main will not have a return value; it will produce an exit code which is the exit code of echo $current. The function begins by calling get_last_name inside a command substitution ($(...)), which will capture all the standard output from get_last_name and treat it as a string.

    DIGRESSION

    Note the difference between the following:

    current=$(get_last_name "$path")
    

    sets the value of current to the accumulated standard output of get_last_name. (Among other things, newlines in the output are replaced with spaces, but the full explanation of how whitespace is handled is a topic for another day). This has nothing to do with return values; remember, the exit code (the closet thing bash has to “return values”) is a single integer.

    current=get_last_name "$path"
    

    would not even call get_last_name. It would interpret “$path” as the name of a command and try to execute it. That command would have a variable current with the string value “get_last_name” in its environment.

    The point being, get_last_name doesn’t “return” anything that you can assign to a variable. It has an exit code, and it can write to standard output. The $(...) construct lets you capture that output as a string, which you can then (among other things) assign to a variable.

    Back to main

    Once the value of current is set to the output generated by get_last_name, we execute
    two last echo statements to write to standard output again. The first writes “Current=” without a newline, so that the next echo statement produces text on the same line as the first. The second just echoes the value of current.

    When you commented out the last echo of main, you didn’t stop get_last_name from being executed (it had already been executed). Rather, you just didn’t print the contents of the current variable, where the output of get_last_name was placed rather than on the terminal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get the value from an inner function. Why is domain
Trying to P/Invoke the SetFileTime function from my C# program, I am using the
Some overflow runtime error happens when my C++ program is trying to write some
I am trying to program a noise reduction algorithm that works with a set
I'm trying to program a graph class using an adjacent list from an example
I'm trying to retrieve a boolean value from one of two member functions. I
In a continuation from this question . I'm trying to bind a given function
I am trying to load some data from a text file into a vector
I'm trying to code some basic kernel module - userspace program communication using netlink
I'm trying to get a string from my managed code into my unmanaged code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.