Echo output to terminal within function in BASH

bash function return array
bash function return boolean
bash print function
bash function output to variable
echo function bash
bash echo
bash function return multiple values
bash function with arguments and return value

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)

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"
}

Bash Scripting echo locally in a function, Anything that's printed by the function can be captured if you capture the right output stream. So the easiest way to print something and save  Using Commands With echo. We can use a command with echo and incorporate its output into the string that is written to the terminal window. We must use the dollar sign $ as though the command was a variable, and wrap the whole command in parentheses.

send terminal output to stderr:

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

Print echo and return value in bash function, Well, depending on what you wish, there are several solutions: Print the message to stderr and the value you wish to take in stdout . function  Echo output to terminal within function in BASH. 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.

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

Shell Command / Script To Write Simple Output On Screen Under , echo "Hello World!" You can add echo command to your shell script: #!/bin/bash echo "Memory  echo is one of the most commonly and widely used built-in command for Linux bash and C shells, that typically used in scripting language and batch files to display a line of text/string on standard output or a file. echo command examples. The syntax for echo is: echo [option (s)] [string (s)] 1. Input a line of text and display on standard output.

Howto: Call Shell Script function In echo statement, I need to call the function called foo() using echo command in the same /proc/​cpuinfo | uniq | awk -F':' '{ print $2}' } findkernelversion(){ uname  H ow do I simply write output from my shell script to the screen under Unix / Linux BASH shell? Most modern shell such as bash, ksh and others comes with the echo and print in built commands. There is also printf (C like statement) command to format and display data on screen. My website is made possible by displaying online advertisements to

Bash Functions, When a bash function ends its return value is its status: zero for success, non-​zero for function myfunc() { myresult='some value' } myfunc echo $myresult Here the result is output to the stdout and the caller uses command  The echo command is one of the most basic and frequently used commands in Linux. The arguments passed to echo are printed to the standard output. echo is commonly used in shell scripts to display a message or output the results of other commands. echo is a shell builtin in Bash and most of the other popular shells like Zsh and Ksh.

Returning Values from Bash Functions, When the user runs any command from the terminal then it shows the output if no error do echo "$dirname" done. Run the script. $ bash cmdsub2.sh. Output:  Howto: Call Shell Script function In echo statement last updated January 21, 2011 in Categories BASH Shell I need to call the function called foo() using echo command in the same statement.

Comments
  • Would it be OK to just call test_function directly, rather than execute it within a subshell $(test_function)?
  • 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?
  • 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,
  • 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...
  • 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.