Search⌘ K
AI Features

Putting It Together - Some Useful Examples

Explore practical Bash examples to apply your shell knowledge effectively. Understand how to output command results with timestamps, manage history, find script locations, and write time-saving functions. This lesson helps you embed key Bash concepts in real-world scenarios to improve your scripting skills.

How Important is this Lesson?

You can easily skip this lesson if you want as nothing here is new, but following this can embed some concepts and keep your motivation going before the final part!

Output With Time

Frequently, I want to get the output of a command along with the time. Let’s say I’m running vmstat while a server is having problems, and I want to ensure that I’ve logged the time that vmstat relates to. Type this in:

Shell
function dateit() {
while read line
do
echo "$line $(date '+ %m-%d-%Y %H:%M:%S')"
done
}
vmstat 1 | dateit
Terminal 1
Terminal
Loading...

Note: vmstat is a program available on most Linux flavours that gives you a view of what the system resource usage looks like. It is not available on Mac OSes. If vmstat does not exist for you, then replace with vm_stat, which should be available.

You should be able to follow what’s happening there based on what you’ve learned so far in