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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:17:22+00:00 2026-05-13T09:17:22+00:00

I’m trying to write a helper function to use in a bash script to

  • 0

I’m trying to write a helper function to use in a bash script to take a variable prompt string and return the value the user inputs. What I have sits and waits for a user to input the value without displaying the prompt first, which is puzzling. It also stores the echo in the return value ($foo), and doesn’t keep the value read in with the pid variable.

!#/bin/bash
pid=0
promptValue() {
  msg="$1"
  echo -e "$msg"
  read val
  pid=$val
}

foo=$(promptValue "type something")

EDIT:
For anyone who might want to use this in the future for their own use, this is the full (functional) script, which is designed to send an email (in this case to my cellphone) to let me know when a long running process completes. I’m sure there has to be a better way to do this, but meh. 🙂 (I wrote them like this to be used in a bash function library elsewhere.)

#!/bin/bash

promptValue() {
 read -p "$1"": " val
 echo $val
}

alertme() {
 if [ -z "$email" ]; then
   email=$(promptValue "Enter email")
 fi

 if [ -z "$email" ]; then 
   echo "ERROR: No email set!"
   exit 1
 fi

 if [ -z "$pid" ]; then
   pid=$(promptValue "Enter pid")
 fi

 if [ -z "$pid" ]; then 
   echo "ERROR: No pid set!"
   exit 1
 fi

 ps -ef | grep $pid | grep -v grep > /dev/null 2>&1
 while [ $? eq 0 ]; do
  sleep 10
  ps -ef | grep $pid | grep -v grep > /dev/null 2>&1
 done

 echo "Process Complete" | mailx -s "Process Complete" $email
}

alertme

Thanks again everyone!

  • 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-13T09:17:23+00:00Added an answer on May 13, 2026 at 9:17 am

    Functions’ exit values are the same as that of the last command executed within the function or the value supplied by a return n statement where n is in the range 0-255. If you want the function to return a string or numeric value, you use echo. If you want to capture the output of a function in a variable and still be able to send output to the user without having that captured, use stderr which is what read -p does.

    $ demo() {
        local num
        echo "capture this"
        echo "Message to user" >&2
        read -p $1 num
        echo $(( num * 2 ))
        return 42
    }
    $ result=$(demo "Enter a number: ")
    Message to user
    Enter a number: 12
    $ echo $?
    42
    $ echo "$result"
    capture this
    24
    $ echo "$num"
    [null][newline]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 441k
  • Answers 441k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You are retrieving jQuery objects when you do the $(..)… May 15, 2026 at 5:38 pm
  • Editorial Team
    Editorial Team added an answer Given that SQLite 3 will use up to 8 bytes… May 15, 2026 at 5:38 pm
  • Editorial Team
    Editorial Team added an answer That’s because the buttons take the touch. When a touch… May 15, 2026 at 5:38 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.