Master Bash: Essential Commands, Aliases, Redirection, and Job Control

This guide explains Bash fundamentals—including command history, aliases, piping, redirection, shortcut keys, job control, and brace expansion—while showing practical examples for efficient Linux shell usage on CentOS 6.3.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Bash: Essential Commands, Aliases, Redirection, and Job Control

Overview of Bash in Linux

A complete computer system consists of hardware and software; the software layer is split into system software (kernel) and application software. Users interact with the system through applications or shell commands, which the kernel translates into hardware operations. In Linux, the shell is the primary interface to the kernel, and 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 teaching example.

1. Command History

Bash automatically records each command in the hidden file ~/.bash_history when the user logs out. History can be viewed 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). Executing history -c clears the history.

Historical commands can be re‑executed in several ways:

Use the up/down arrows to scroll 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 hit Enter to search and execute.

2. Command Aliases

Aliases provide short, memorable shortcuts for longer commands. For example, ll is not a separate executable; it is an alias that expands to ls -l, allowing users to display detailed file information with a single word.

3. Pipes and Redirection

Standard input (fd 0) and standard output (fd 1) are normally the keyboard and screen. Redirection symbols ( <, >, <<, >>, |) let users change these defaults.

The pipe operator ( |) feeds the output of one command directly into the input of the next, e.g., ifconfig eth0 | grep 'inet addr' filters the network interface information for IP addresses.

Output redirection: > file creates or overwrites file with the command’s output. >> file appends the output to file. 2> file redirects error output.

Input redirection: < file reads command input from file.

4. Shortcut Keys

Bash offers many keyboard shortcuts that speed up command‑line work. Learning the most common ones can dramatically improve productivity.

5. Redirection Tricks

When a script generates many success messages that are not needed, redirect the standard output to /dev/null (a black hole) to discard it. This keeps the log focused on error messages.

Separating standard output and error output is useful for nightly automated scripts. By redirecting each stream to a different file, administrators can quickly see which commands succeeded and which failed.

6. Command Sequencing

Linux provides control operators to define execution order: ; – run commands sequentially, regardless of success. && – run the next command only if the previous one succeeds. || – run the next command only if the previous one fails. & – run a command in the background.

Examples: [root@centos6 ~]# firefox Run Firefox in the background so the shell remains usable: [root@centos6 ~]# firefox & Execute several commands in order: [root@centos6 ~]# ls /tmp ; ls /root ; ls /home Conditional execution:

[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"

The last example silences id tom output, then greets the user if the account exists, otherwise reports it does not.

7. Job Control

Appending & to a command runs it in the background. Pressing Ctrl+z pauses a foreground job and moves it to the background. The jobs command lists background jobs with IDs, and fg <ID> brings a job back to the foreground.

[root@centos6 ~]# firefox &
[root@centos6 ~]# jobs
[1]+  Running         firefox &
[root@centos6 ~]# fg 1

8. Brace Expansion

Brace expansion generates multiple strings from a pattern. Inside {}, you can specify a range (e.g., {1..5}) or a comma‑separated list (e.g., {a,b,c}). This is useful for creating command lines or scripts that need repetitive arguments.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Redirectionjob-control
MaGe Linux Operations
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.