Bash function to prompt for and return input?

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.

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" nullnewline.

I ended up using a combination of this answer and Alex's answer above. Thanks gang! – Almo Jan 1 '10 at 21:20.

The normal way to do it would seem to be something like: #! /bin/bash pid=0 promptValue() { read -p "$1"": " val pid=$val } promptValue "type something" echo 'pid:'$pid'! ' $(...), "command substitution", takes the "command"'s standard output as the result (so it doesn't show the echo on your terminal, etc).

The extra quotes aren't necessary: read -p "$1: " val and echo "pid:$pid! " (although at the prompt, the superfluous exclamation point, when in double quotes, will cause a history "event not found error". In a script, this won't be a problem.

– Dennis Williamson Jan 1 '10 at 23:16.

Obviously this will work if you execute directly instead assigning it to foo. If you are trying to capture the value of val then why noit just do: promptValue "type something" foo=$pid.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions