The GNU Bourne-Again Shell (bash) is a program that interprets commands that the user types. Each string that is typed into the shell can have up to three parts: the command, options (which usually begin with a hyphen - or double hyphen -- characters), and arguments. Each word that is typed into the shell is separated from other words with spaces. Commands are the names of programs that are installed on the system. Each command has its options and arguments.
When you are ready to execute a command, press the Enter key. Type each command on a separate line. The command output is displayed before the following shell prompt appears.
[user@host ~]$ whoami user [user@host ~]$ |
To type more than one command on a single line, use the semicolon (;) as a command separator. A semicolon is a member of a class of characters called metacharacters that have a special interpretation for bash. In this case, the output of both commands is displayed before the following shell prompt appears.
The following example shows how to combine two commands (command1 and command2) on the command line.
[user@host ~]$ command1 ; command2 command1 output command2 output [user@host ~]$ |
Write Simple Commands
The date command displays the current date and time. The superuser or a privileged user can also use the date command to set the system clock. Use the plus sign (+) as an argument to specify a format string for the date command.
[user@host ~]$ date Sun Feb 27 08:32:42 PM EST 2022 [user@host ~]$ date +%R 20:33 [user@host ~]$ date +%x 02/27/2022 |
The passwd command with no options changes the current user's password. To change the password, first specify the original password for the account. By default, the passwd command is configured to require a strong password, to consist of lowercase letters, uppercase letters, numbers, and symbols, and not to be based on a dictionary word. A superuser or privileged user can use the passwd command to change another user's password.
[user@host ~]$ passwd Changing password for user user. Current password: old_password New password: new_password Retype new password: new_password passwd: all authentication tokens updated successfully. |
View the Contents of Files
The cat command is often used in Linux. Use this command to create single or multiple files, view the contents of files, concatenate the contents from various files, and redirect contents of the file to a terminal or to files.
The following example shows how to view the contents of the /etc/passwd file:
[user@host ~]$ cat /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin ...output omitted... |
To display the contents of multiple files, add the file names to the cat command as arguments:
[user@host ~]$ cat file1 file2 Hello World!! Introduction to Linux commands. |
Some files are long and might need more space to be displayed than the terminal provides. The cat command does not display the contents of a file as pages. The less command displays one page of a file at a time and you can scroll at your leisure.
Use the less command to page forward and backward through longer files than can fit on one terminal window. Use the UpArrow key and the DownArrow key to scroll up and down. Press q to exit the command.
The head and tail commands display the beginning and the end of a file, respectively. By default, these commands display 10 lines of the file, but they both have a -n option to specify a different number of lines.
Understand Tab Completion
With tab completion, users can quickly complete commands or file names after typing enough at the prompt to make it unique. If the typed characters are not unique, then pressing the Tab key twice displays all commands that begin with the typed characters.
Tab completion helps to complete file names when typing them as arguments to commands. Press Tab to complete as much of the file name as possible. Pressing Tab a second time causes the shell to list all files that the current pattern matches. Type additional characters until the name is unique, and then use tab completion to complete the command.
Use the useradd command to create users on the system. The useradd command has many options that might be hard to remember. By using tab completion, you can complete the option name with minimal typing.
[root@host ~]# useradd --Tab+Tab* --badnames --gid --no-log-init --shell --base-dir --groups --non-unique --skel --btrfs-subvolume-home --help --no-user-group --system --comment --home-dir --password --uid --create-home --inactive --prefix --user-group --defaults --key --root --expiredate --no-create-home --selinux-user *Press Tab twice. |
Display the Command History
The history command displays a list of previously executed commands that are prefixed with a command number.
The exclamation point character (!) is a metacharacter to expand previous commands without retyping them. The !number command expands to the command that matches the specified number. The !string command expands to the most recent command that begins with the specified string.
The arrow keys help to navigate through previous commands in the shell's history. The UpArrow edits the previous command in the history list. The DownArrow edits the next command in the history list. The LeftArrow and RightArrow move the cursor left and right in the current command from the history list so that you can edit the command before running it.
Edit the Command Line
When used interactively, bash has a command-line editing feature. Use the text editor commands to move around and modify the currently typed command. Using the arrow keys to move within the current command and to step through the command history was introduced earlier in this section. The following table shows further powerful editing commands.
Useful Command-line Editing Shortcuts
Shortcut | Description |
|---|---|
Ctrl+A | Jump to the beginning of the command line. |
Ctrl+E | Jump to the end of the command line. |
Ctrl+U | Clear from the cursor to the beginning of the command line. |
Ctrl+K | Clear from the cursor to the end of the command line. |
Ctrl+LeftArrow | Jump to the beginning of the previous word on the command line. |
Ctrl+RightArrow | Jump to the end of the next word on the command line. |
Ctrl+R | Search the history list of commands for a pattern. |
These command-line editing commands are the most helpful for new users. For other commands, refer to the bash(1) man page.
Comments
Post a Comment