On Linux, you can display the execution duration of a command while it is running using several methods. Here are the most practical approaches:
time
PROMPT_COMMAND
You can configure your shell to show the duration of each command as it runs by modifying your ~/.bashrc or ~/.bash_profile:
~/.bashrc
~/.bash_profile
# Add to ~/.bashrc or ~/.bash_profile PROMPT_COMMAND='if [ "$(declare -p CMD_START)" ]; then echo "Duration: $((SECONDS - CMD_START))s"; unset CMD_START; fi' trap 'CMD_START=$SECONDS' DEBUG
How it works:
DEBUG
CMD_START
Drawback: It shows the duration after the command finishes, not during.
date
If you want to see the duration as the command is running, you can use a loop with date:
start=$(date +%s) while true; do sleep 1 duration=$(( $(date +%s) - start )) echo -ne "Elapsed time: $duration seconds\r" done
Run this in a separate terminal or background it, then run your command in another terminal.
expect
You can use expect to wrap your command and print the duration:
#!/usr/bin/expect -f set start [clock seconds] spawn your_command_here expect eof set end [clock seconds] puts "Duration: [expr $end - $start]s"
Save as run_with_duration.sh, make it executable, and run it.
run_with_duration.sh
script
If you want to log everything, including the duration:
script -q -c "your_command_here" /dev/null
The duration is not shown in real-time, but you can calculate it afterward.
The simplest way to see the duration after the command finishes:
time your_command_here
This will output something like:
real 0m1.003s user 0m0.001s sys 0m0.002s
tput
If you want a real-time counter in the terminal, you can use a script like this:
#!/bin/bash start=$(date +%s) your_command_here & pid=$! while kill -0 $pid 2>/dev/null; do duration=$(( $(date +%s) - start )) echo -ne "Elapsed time: $duration seconds\r" sleep 1 done echo -e "\nCommand finished after $duration seconds"
Which method do you prefer or need help setting up?
This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.
Vibe can make mistakes. Check answers. Learn more