Executing,Commands,with Bash,01
Commands with Bash
RED HAT SYSTEM ADMINISTRATION I (RH124) – CHAPTER 2: EXECUTING COMMANDS WITH BASH
Understanding Bash Command Structure
- The structure of a Bash command includes:
- Commands: The main program or command to be executed.
- Options: Modifications to commands that modify their behavior and usually start with a
- or --. - Arguments: The input on which the command operates, representing targets or resources the command will manipulate.
- The fundamental components of a command are tied to the shell’s execution model.
Commands, Options, and Arguments
- Commands consist of program names located in the PATH environment variable.
- Examples of commands include:
- date: Displays the current date and time.
- passwd: Changes the user's password.
- file: Identifies file types.
- Special characters used to control command execution and separate commands.
BASIC COMMAND SYNTAX
Components of Command Syntax
- Explanation of command components:
- Commands: Names of executable programs located in system PATH, which users can invoke.
- Options: Modify how the command behaves;
- Single hyphen (
-) for single-letter options. - Double hyphen (
--) for full-word options. - Arguments: Inputs or targets for the command to act upon.
VIEWING FILE CONTENTS
File Display Commands
- Various commands used to view contents of files:
- cat:
- Concatenates and displays the content of files.
- Usage:
cat filename - less:
- Provides paginated viewing capabilities, allowing navigation through large files.
- Usage:
less filename - head:
- Displays the first ten lines of a file by default.
- Usage:
head filename - tail:
- Displays the last ten lines of a file by default.
- Usage:
tail filename - wc (word count):
- Counts the number of lines, words, and bytes in a file.
- Syntax:
wc filename, where options can be specified for particular counts (lines, words, bytes).
TAB COMPLETION
- Tab Completion: A feature that helps speed up typing commands and file paths.
- Pressing the Tab key once attempts to auto-complete the typed command or file name.
- Pressing Tab twice displays a list of potential matches for completion.
- This functionality is particularly beneficial for long commands and file paths, reducing error and time spent typing.
WRITING LONG COMMANDS
Breaking Long Commands into Multiple Lines
- When commands exceed screen length, users can write long commands more manageably:
- Utilize a backslash (
\) at the end of a line to indicate the command continues on the next line. - The shell responds with a continuation prompt, signaling expectation of more input.
COMMAND HISTORY
Navigating and Utilizing Command History
- The history command allows users to view previously executed commands.
- Syntax:
history - Users can recall commands using:
!number: Execute the command at the specified position in history. !string: Repeat the most recent command starting with the specified string. - Navigation through previous commands can also be done using the Arrow keys.
- The Alt + . shortcut lets users insert the last argument from the previous command.