Lesson · 40 min · Free
Shell Command History & Editing
Shell Command History & Editing body { font-family: sans-serif; line-height: 1.6; margin: 20px; } h1, h2 { color: #2c3e50; } pre { background-color: #ecf0f1; padding: 15px; border-radius: 5px; overflow-x: auto; } code {
Shell Command History & Editing
In bioinformatics and computational genomics, efficiency at the command line is paramount. You'll often find yourself repeating similar commands, correcting typos, or recalling complex pipelines. The shell's history and editing features are powerful tools that can significantly boost your productivity and reduce repetitive typing. Understanding these features will allow you to navigate your command-line sessions with greater agility, making your computational work more streamlined and less prone to errors. This lesson will cover how to access previous commands, modify them without retyping, and leverage keyboard shortcuts for faster interaction with your shell. While most modern shells (like Bash and Zsh) offer similar functionalities, we'll focus on features common to Bash, which is widely used in bioinformatics environments.
Accessing and Reusing Command History
Every command you execute in your shell session is recorded in a history file, typically ~/.bash_history for Bash. This history is not just for viewing; it's a dynamic resource you can interact with. The simplest way to access your command history is using the history command. This will display a numbered list of your previously executed commands. history You can pipe the output of history to other commands like grep to search for specific commands you've run before. This is particularly useful when you remember part of a complex command but not the whole thing. history | grep "samtools view" Once you've identified a command in your history, there are several ways to re-execute it: !! : This executes the last command you ran. Extremely useful for quickly repeating a command or fixing a permission error (e.g., sudo !! ). !n : Executes the command corresponding to number n in the history list (e.g., !123 ). !-n : Executes the n th command back from the current position in history (e.g., !-2 executes the second to last command). !string : Executes the most recent command that starts with string (e.g., !ls ). !?string? : Executes the most recent command that contains string (e.g., !?samtools? ). Arrow Keys (Up/Down): These are perhaps the most frequently used. The Up Arrow cycles backward through your history, and the Down Arrow cycles forward.
Command Line Editing and Expansion
Beyond simple recall, the shell provides powerful editing features that allow you to modify previous commands or construct new ones efficiently. Ctrl+R (Reverse-i-search): This is a highly effective way to search your history. Pressing Ctrl+R will display a prompt ( (reverse-i-search)`': ). As you type, the shell will search backward through your history and display the most recent command matching your input. You can press Ctrl+R repeatedly to cycle through older matches. Press Enter to execute the displayed command, or the left/right arrow keys to edit it before execution. History Expansion: This allows you to extract specific parts of previous commands. !$ : Refers to the last argument of the previous command. For example, if you typed cp file.txt /path/to/destination/ , then cd !$ would become cd /path/to/destination/ . !^ : Refers to the first argument of the previous command. !:n : Refers to the n th argument of the previous command. !:0 refers to the command itself. !* : Refers to all arguments of the previous command. !:s/old/new/ : Substitutes old with new in the previous command. For instance, if you typed ls -l my_data.txt and then realized it should be my_data_final.txt , you could type !:s/my_data/my_data_final/ . Add a g for global replacement ( !:gs/old/new/ ). These editing features, especially Ctrl+R and history expansion, can save you an immense amount of time when working with long file paths, complex command-line options, or when quickly correcting a typo. Mastering them is a hallmark of an efficient command-line user.
Key Takeaways
The history command displays your past shell commands. Use !! , !n , !string , or arrow keys to quickly re-execute commands. Ctrl+R (reverse-i-search) is an efficient way to search and recall commands from history. History expansion (e.g., !$ , !:s/old/new/ ) allows for powerful modification and reuse of command arguments. These features significantly improve command-line efficiency and reduce typing errors.
Practice Exercise
Imagine you are analyzing RNA-seq data. You first ran a command to count reads for a sample: featureCounts -a annotation.gtf -o counts_sample1.txt -T 8 sample1_aligned.bam . Now, you realize you need to run the exact same command for sample2_aligned.bam , but output to counts_sample2.txt . Using only history and editing features (no retyping the full command), how would you execute the command for sample2 ? Provide the specific history command(s) you would use.
Watch the full lesson — free
This topic is part of Bioinformatics & Computational Genomics, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →