Master Bash: Essential Commands, History, Aliases, Redirection & Tips
This article explains the core concepts of Bash on Linux, covering command history, alias creation, input/output redirection, pipelines, shortcut keys, job control, and brace expansion, with practical examples and usage tips for CentOS 6.3 users.
Overview of Computer Architecture and Shell Interaction
A complete computer system consists of hardware and software; the kernel of system software manages hardware while users interact through applications or shell commands. In Linux, the shell (e.g., Bash) is the primary interface for issuing commands that the kernel translates into hardware actions.
Bash Features on CentOS 6.3
1. Command History
Bash automatically records each command in ~/.bash_history. The number of stored entries is controlled by the HISTSIZE variable (default 1000 on CentOS 6.3). History can be viewed by opening the file, using the up/down arrow keys, or running the history command, which prefixes each entry with a number.
Recall a command with the up/down arrows and press Enter to execute.
Use !string to run the most recent command starting with string (e.g., !vim).
Use !n to run the command with number n (e.g., !242).
Press Ctrl+r, type a keyword, and press Enter to search the history; Esc aborts the search.
2. Command Aliases
Aliases let you create short names for long commands. For example, ll is an alias for ls -l on CentOS. Defining aliases reduces typing and speeds up repetitive tasks.
3. Pipes and Redirection
Bash’s standard input (fd 0) is usually the keyboard, and standard output (fd 1) and error (fd 2) go to the screen. Redirection symbols ( <, >, >>, <<, |) let you change these streams.
Using a pipe ( |) connects the output of one command to the input of the next, e.g., ifconfig eth0 | grep 'inet addr' filters the IP address line.
Redirection examples: command > file writes output to file (overwrites). command >> file appends output to file. command 2> err redirects error output. command < input reads input from input file.
4. Shortcut Keys
Bash provides many keyboard shortcuts that improve efficiency. Memorizing them speeds up navigation, editing, and command execution.
5. Redirection Techniques
When a script generates many success messages, you may only care about errors. Redirect normal output to /dev/null to discard it: script.sh > /dev/null 2>&1 Separate standard and error output into different files for later analysis:
command > success.log 2> error.log6. Command Sequencing
Control characters determine how multiple commands are executed: ; – run commands sequentially regardless of success. && – run the next command only if the previous succeeds. || – run the next command only if the previous fails. & – run a command in the background.
ls /tmp ; ls /root ; ls /home
ls test.txt && cat test.txt
gedit || vim
id tom &>/dev/null && echo "Hi,tom" || echo "No such user"
7. Job Control
Appending & runs a process in the background. Ctrl+z suspends a foreground job, jobs lists background jobs, and fg %n brings job n back to the foreground.
firefox & jobs fg 1
8. Brace Expansion
Brace expansion generates multiple strings from a pattern, using commas or a range with ... It is useful for creating command lists quickly.
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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
