Of course! This is an excellent and common question because it highlights a key difference between shell built-ins and external commands.
Here’s a complete guide on how to use GNU time to measure a subshell, explaining why the direct approach doesn't work and how the correct solution functions.
The TL;DR (The Quick Answer)
To measure a group of commands running in a subshell with GNU time, you must launch a new shell instance to execute them.
# General syntax
/usr/bin/time sh -c '(command1; command2; ...)'
# Or using the \ escape to ensure you get the external command
\time sh -c '(cd /tmp; sleep 1; echo "Done in /tmp")'
The Key Challenge: External Command vs. Shell Metacharacter
The reason you can't just write /usr/bin/time ( ... ) is due to how the shell parses commands.
-
Shell's Built-in time: The time keyword that's part of your shell (like bash or zsh) is a special construct. The shell knows how to apply it to pipelines, command groups, and subshells directly.
# This works because 'time' is a shell keyword
time (cd /; sleep 1)
-
GNU time (External Command): The GNU time utility is an external program, usually located at /usr/bin/time. Like any other program (ls, grep, cat), it expects its arguments to be the name of another command followed by that command's arguments.
The characters ( and ) are shell metacharacters. They are interpreted by your current shell to mean "start a subshell". They are not a command that can be passed as an argument to /usr/bin/time.
If you try this, you'll get a syntax error from your shell before /usr/bin/time even runs:
# This will FAIL
/usr/bin/time (echo "hello"; sleep 1)
# Shell output:
# bash: syntax error near unexpected token `('
The shell sees /usr/bin/time and then (, and it doesn't know how to connect them.
The Solution: sh -c
The correct way to solve this is to package your entire subshell command into a single string and pass it to a new shell process. This is the purpose of sh -c.
Let's break down the working command:
/usr/bin/time sh -c '(cd /tmp; sleep 1; ls -l > /dev/null)'
- /usr/bin/time: This is the GNU
time program we want to run. It will measure the execution of the command that follows.
sh: This is the command that time will measure. We are telling time to execute a new shell.
-c: This is an option for sh (and bash, zsh, etc.) that tells the shell to read and execute commands from the next argument (a string), rather than from standard input or a file.
'(...)': This is the single string argument passed to sh -c.
- The single quotes (
' ') are crucial. They prevent your current shell from interpreting the parentheses, semicolons, or any other special characters inside. The entire string is passed literally to the new sh process.
- The new
sh process then receives this string, parses it, and executes the commands within it, including correctly creating the subshell (...).
gnu time measures the total time taken for the sh -c '...' process to complete, which is exactly what we want.
Practical Examples
1. Basic Subshell Measurement
Let's measure a sequence of commands in a subshell. We'll use the -v (verbose) flag of GNU time to get detailed output.
# We use '\time' as a shortcut to bypass the shell's built-in time
\time -v sh -c '(echo "Starting in subshell..."; sleep 1.5; echo "Finished!")'
Output:
Starting in subshell...
Finished!
Command being timed: "sh -c (echo "Starting in subshell..."; sleep 1.5; echo "Finished!")"
User time (seconds): 0.00
System time (seconds): 0.00
Percent of CPU this job got: 0%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:01.51
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 1848
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 82
Voluntary context switches: 4
Involuntary context switches: 2
Swaps: 0
File system inputs: 0
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
As you can see, the Elapsed (wall clock) time is approximately 1.5 seconds, as expected.
2. Measuring a Pipeline Inside a Subshell
This technique is also perfect for timing an entire pipeline, as pipelines also run in subshells.
# Time how long it takes to find all files and count them
\time -v sh -c '(find /usr/bin -type f | wc -l)'
3. Redirecting time Output
One of the best features of GNU time is redirecting its timing data to a file without affecting the subshell's own output.
# -o sends time data to 'timing.log'
# The subshell's output ("All done.") goes to the terminal
\time -o timing.log sh -c '(echo "Running complex task..."; sleep 1; echo "All done.")'
After running this:
4. Using bash for Bash-Specific Syntax
If your subshell contains commands that are specific to bash (like [[ ... ]] or arrays), simply use bash -c instead of sh -c.
# This uses a bash-specific feature (process substitution)
\time -v bash -c 'diff <(echo "a") <(echo "b")'