Display output of command executed in bash as it is executing
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
Related videos on Youtube
JRodDynamite
Updated on September 18, 2022Comments
-
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 over 6 yearsJust run the command?
-
Rooney over 6 yearsIt would be better if you could paste the commands you are running.
-
JRodDynamite over 6 years@fkraiem - I need to perform certain checks before I can execute the command.
-
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 over 6 yearsWhy are you wrapping
some_command
in a command substitution andecho
ing the result? Just executesome_command
directly. -
sudodus over 6 yearsYou can simply put an 'echo line' before the executing line:
echo "some command"
orecho "This command is running now ... (please wait for it to finish)"
, which will print the information you want (in the terminal window).
-