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 910031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:58:43+00:00 2026-05-15T16:58:43+00:00

Example: #!/bin/sh a() { R=f ls -1 a* [ $? == 1 ] &&

  • 0

Example:

#!/bin/sh

a() {
R=f
ls -1 a*
[ "$?" == "1" ] && { R=t; }
echo $R
}

r=`a`
echo $r

$r contains t or f but also the output of the ls command.

I may write ls -1 a* >/dev/null 2>/dev/null, but if there is a more complex script that can be lead to errors.

Is there any way to return a single value from a() ?

  • 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-05-15T16:58:44+00:00Added an answer on May 15, 2026 at 4:58 pm

    A shell function can return a numeric value. Consider 0 and 1 rather than ‘f’ and ‘t’

    #!/bin/sh
    
    a() {
    R=0
    ls -1 a*
    [ "$?" == "1" ] && { R=1; }
    return $R
    }
    
    a
    r=$?
    echo $r
    

    This will still write output from the ls -1 a* which you probably still want to dispose of, but the value of r will be either 0 or 1 and won’t include the output.

    The other examples of redirecting output either from a line or a whole block are good, and, as others suggested, you should get to know other ways to test for conditions (but I was assuming the ls was kind of an arbitrary example)

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

Sidebar

Related Questions

I'm attempting a pretty cut & dry example of anydbm: #!/usr/bin/python import anydbm #
A url looks like: http://www.example.com/cgi-bin/blahblah?&PC=abd23423&uy=020 I need to extract the value: abc23423 I tried
Take a look at my code example at js bin: http://jsbin.com/iritep/3/edit I'd like to
The code #!/usr/bin/awk # Sed and AWK by O'Reilly (p.179) # Example of what
I have this shell script #!/bin/sh ############################################################# # Example startup script for the SecureTrading
I have names of all files in output.txt but I do not have folderpath
As an example consider the following bash script. There are two loops, the first
Given this Unix shell script: test.sh: #!/bin/sh sleep 2 & sleep 5 & sleep
I have included an example I am trying to test here but since I
this site is extremely cool ( http://www.gsp.com/cgi-bin/man.cgi?section=3&topic=strncpy ) but a bit unformatted, it shows

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.