Master Bash: Essential Commands, Aliases, Redirection, and Job Control
This guide explains Bash’s core concepts—including command history, aliases, pipes, redirection, shortcut keys, advanced redirection tricks, command sequencing, job control, and brace expansion—providing Linux users with practical examples to boost productivity and streamline system operations.
Bash Overview
A complete computer architecture consists of hardware and software; the software layer is divided into system software and application software. Users interact with the system through applications or system utilities, which translate commands for the kernel, and the kernel schedules hardware resources to fulfill those requests.
In Linux, users typically communicate with the kernel via a shell. CentOS 6.3 provides several shells such as /bin/sh, /bin/bash, /bin/tcsh, and /bin/csh. This article uses the default Bash shell as the example.
1. Command History
Bash automatically records each command to ~/.bash_history when the session ends. Users can view history by opening the file, using the up/down arrow keys, or running the history command, which shows numbered entries. The number of stored commands is controlled by the HISTSIZE variable (default 1000 on CentOS 6.3). The history -c command clears the history.
History entries can be re‑executed directly:
Use the up/down arrows to select a command and press Enter.
Type !string to run the most recent command starting with string (e.g., !vim).
Use !n to run command number n (e.g., !242).
Press Ctrl+r, type a keyword, and press Enter to search and execute a matching command.
2. Command Aliases
Aliases simplify long commands. For example, ll is an alias for ls -l on CentOS 6.3, allowing users to type a short, memorable name instead of the full command.
3. Pipes and Redirection
Bash’s standard input (fd 0) is typically the keyboard, and standard output (fd 1) and error (fd 2) go to the screen. Redirection symbols ( <, >, <<, >>, |) change these defaults.
The pipe operator ( |) feeds the output of one command into the input of the next, e.g., ifconfig eth0 | grep 'inet addr' filters the network interface information to show only lines containing an IP address.
Output redirection: >file writes output to file, overwriting it. >>file appends output to file. 2>file redirects error output.
Input redirection uses <file to read data from a file.
Examples are illustrated in the following image:
4. Shortcut Keys
Bash provides many keyboard shortcuts that speed up command‑line work. Mastering them greatly improves efficiency.
5. Redirection Tricks
1. To discard unimportant output, redirect it to /dev/null (e.g., command > /dev/null).
2. Separate standard output and error output into different files:
Check if user tom exists; if so, write info to user, otherwise write to error:
id tom &>/dev/null && echo "Hi,tom" || echo "No such user"6. Command Sequencing
Control characters allow complex command flows: ; – execute commands sequentially regardless of success. && – execute the next command only if the previous one succeeds. || – execute the next command only if the previous one fails. & – run a command in the background.
Examples:
[root@centos6 ~]# firefox [root@centos6 ~]# firefox & [root@centos6 ~]# ls /tmp ; ls /root ; ls /home [root@centos6 ~]# ls test.txt && cat test.txt [root@centos6 ~]# gedit || vim [root@centos6 ~]# id tom &>/dev/null && echo "Hi,tom" || echo "No such user"7. Job Control
Appending & runs a process in the background. Ctrl+z pauses a foreground job and moves it to the background. Use jobs to list background jobs, and fg %n to bring job n back to the foreground.
[root@centos6 ~]# firefox & [root@centos6 ~]# jobs [1]+ Running firefox & [root@centos6 ~]# fg 18. Brace Expansion {}
Brace expansion generates multiple strings from a pattern, using commas or a range with ... It is useful for creating command lists or file names.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
