Display output of command executed in bash as it is executing

15,122

Solution 1

Replace:

echo "`some_command`"

With:

some_command

The backticks aren't useful here, so can be dispensed with. They'd only be good if the thing needed was to somehow summarize the output after it was completed.

Solution 2

xtrace

You could set the xtrace option (commonly used for debugging shell scripts) before running the command (no need to use command substitution). Also, don’t forget to unset the option afterwards.

set -x
some_command
set +x

Example:

$ set -x; sleep 2 && echo fin; set +x
+ sleep 2
+ echo fin
fin
+ set +x

echo

If you find the xtrace output to be too cluttered, you could simply echo the name of the command beforehand:

echo some_command
some_command
Share:
15,122

Related videos on Youtube

JRodDynamite
Author by

JRodDynamite

Updated on September 18, 2022

Comments

  • JRodDynamite
    JRodDynamite over 1 year

    I've got a line in my bash script like this:

    echo "`some_command`"
    

    Now, some_command executes for around 1-2mins and it keeps printing alerts/messages in that interval.

    Problem is when I execute my bash script, the script has no output for 1-2mins and then directly displays the output after complete execution of some_command.

    How can I make my bash script output some_command as it is executing? Is there a way to do so?

    • fkraiem
      fkraiem over 6 years
      Just run the command?
    • Rooney
      Rooney over 6 years
      It would be better if you could paste the commands you are running.
    • JRodDynamite
      JRodDynamite over 6 years
      @fkraiem - I need to perform certain checks before I can execute the command.
    • JRodDynamite
      JRodDynamite over 6 years
      @Rooney - The command which I'm using is a Python script. I'm calling just one command and then the Python script performs it's task.
    • steeldriver
      steeldriver over 6 years
      Why are you wrapping some_command in a command substitution and echoing the result? Just execute some_command directly.
    • sudodus
      sudodus over 6 years
      You can simply put an 'echo line' before the executing line: echo "some command" or echo "This command is running now ... (please wait for it to finish)" , which will print the information you want (in the terminal window).