35

I am writing a script in BASH. I have a function within the script that I want to provide progress feedback to the user. Only problem is that the echo command does not print to the terminal. Instead all echos are concatenated together and returned at the end.

Considering the following simplified code how do I get the first echo to print in the users terminal and have the second echo as the return value?

function test_function {
    echo "Echo value to terminal"
    echo "return value"
}

return_val=$(test_function)
2
  • Would it be OK to just call test_function directly, rather than execute it within a subshell $(test_function)? Commented Jan 5, 2015 at 9:42
  • Maybe... I didn't realise wrapping in parentheses caused it to run in a separate shell. So your thinking something like what nu11p01n73R mentioned below? Commented Jan 5, 2015 at 21:57

4 Answers 4

31

Yet a solution other than sending to STDERR (it may be preferred if your STDERR has other uses, or possibly be redirected by the caller)

This solution direct prints to the terminal tty:

function test_function {
    echo "Echo value to terminal" > /dev/tty
    echo "return value"
}

-- update --

If your system support the tty command, you could obtain your tty device from the tty command, and thus you may:

echo "this prints to the terminal" > `tty`
Sign up to request clarification or add additional context in comments.

6 Comments

I like this solution... But is /dev/tty going to be the same on every machine? Is there a way to get this location dynamically?
For exception cases, you need to detect the OS, try uname -a, man uname, or echo $OS. Or, perhaps you need to detect the distribution (e.g. Linux). The tty device file is not guaranteed to be in the same place.
But even in Windows, Cygwin has /dev/tty. :-)
Sorry to revive an old question, but would this also work when being captured by say Systemd?
I don't know why it can be related to Systemd, neither am I familiar to Systemd. Sorry. Perhaps you should raise a new question,
|
19

send terminal output to stderr:

function test_function {
    echo "Echo value to terminal" >&2
    echo "return value"
}

4 Comments

bash seems to open the tty also on descriptor &255 so if stderr is also redirected you can message the user using >&255 instead of >&2
that's not stable/reliable behavior -- if it's not covered in documentation, it's not guaranteed to remain true in future versions, or even to be true in all runtime configurations (ie. to be consistent between interactive and noninteractive shells).
I wouldn't think it would be correct to direct non errors to stderr. Couldn't this supply error logs with non errors?
standard error is used for anything other than the main results stream, eg the bash 'select' built-in uses it to present a list to the user. many other programs send their help message when requested to standard error. jpeg2pnm may output EXIF data to stderr...
5

If you don't know in which terminal/device you are:

function print_to_terminal(){
    echo "Value" >$(tty)
}

Comments

4

Dont use command substitution to obtain the return value from the function

The return value is always available at the $? variable. You can use the variable rather than using command substitution

Test

$ function test_function {
> return_val=10; 
> echo "Echo value  to terminal $return_val";
> return $return_val; 
> }

$ test_function
Echo value  to terminal 10

$ return_value=$?

$ echo $return_value
10

4 Comments

I considered this solution but the echo statements seem to print out all at once after the function has finished running (am I correct in saying this?). Consider my function is very processor intensive and long. I want it to provide the user with progress updates while it runs.
Tom Fenech posted a comment to my OP saying that I was running the function in a subshell. I didn't realise this is what I was doing. So this could work.
@McShaman The command substitution $() creates a new subshell. increasing the load
The question was how to get the result (a string) back to the caller, while at the same time be able to write to the console. Not how to get the return code. Your answer does not return a string at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.