The GNU Bourne-Again Shell
(bash) is a program that interprets
commands typed in by the user. Each string typed into the shell can
have up to three parts: the command, options (that begin with a -
or --), and arguments. Each word typed into the shell is separated
from each other with spaces. Commands are the names of programs
that are installed on the system. Each command has its own options
and arguments.
The Enter key is pressed when a user is ready to execute a command. Each command is typed on a separate line and the output from each command displays before the shell displays a prompt. If a user wants to type more than one command on a single line, a semicolon, ;, can be used as a command separator. A semicolon is a member of a class of characters called metacharacters that has special meanings for bash.
The Enter key is pressed when a user is ready to execute a command. Each command is typed on a separate line and the output from each command displays before the shell displays a prompt. If a user wants to type more than one command on a single line, a semicolon, ;, can be used as a command separator. A semicolon is a member of a class of characters called metacharacters that has special meanings for bash.
Examples of Simple Commands
The date command is used to display the current
date and time. It can also be used by the superuser to set the
system clock. An argument that begins with a plus sign (+) specifies
a format string for the date command.
The passwd command changes a user's own password. The original password for the account must be specified before a change will be allowed. By default, passwd is configured to require a strong password, consisting of lowercase letters, uppercase letters, numbers, and symbols, and is not based on a dictionary word. The superuser can use the passwd command to change other users' passwords.[student@desktopX ~]$
date
Sat Apr 5 08:13:50 PDT 2014[student@desktopX ~]$
date +%R
08:13[student@desktopX ~]$
date +%x
04/05/2014
Linux does not require file name extensions to classify files by type. The file command scans the beginning of a file's contents and displays what type it is. The files to be classified are passed as arguments to the command.[student@desktopX ~]$
passwd
Changing password for user student. Changing password for student. (current) UNIX password:New password:
old_password
Retype new password:
new_password
passwd: all authentication tokens updated successfully.
new_password
The head and tail commands display the beginning and end of a file respectively. By default, these commands display 10 lines, but they both have a -n option that allows a different number of lines to be specified. The file to display is passed as an argument to these commands.[student@desktopX ~]$
file /etc/passwd
/etc/passwd: ASCII text[student@desktopX ~]$
file /bin/passwd
/bin/passwd: setuid ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=0x91a7160a019b7f5f754264d920e257522c5bce67, stripped[student@desktopX ~]$
file /home
/home: directory
The wc command counts lines, words, and characters in a file. It can take a -l, -w, or -c option to display only the lines, words, or characters, respectively.[student@desktopX ~]$
head /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 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin[student@desktopX ~]$
tail -n 3 /etc/passwd
gdm:x:42:42::/var/lib/gdm:/sbin/nologin gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin
[student@desktopX ~]$
wc /etc/passwd
39 70 2005 /etc/passwd[student@desktopX ~]$
wc -l /etc/passwd ; wc -l /etc/group
39 /etc/passwd 63 /etc/group[student@desktopX ~]$
wc -c /etc/group /etc/hosts
843 /etc/group 227 /etc/hosts 1070 total
Tab Completion
Tab completion allows a user to quickly
complete commands or file names once they have typed enough at the
prompt to make it unique. If the characters typed are not unique,
pressing the Tab key twice displays all commands
that begin with the characters already typed.
Tab completion can be used to complete file names when typing them as arguments to commands. When Tab is pressed, it will complete the file name as much as it can. Pressing Tab a second time causes the shell to list all of the files that are matched by the current pattern. Type additional characters until the name is unique, then use tab completion to finish off the command line.[student@desktopX ~]$
pas<Tab><Tab>
passwd paste pasuspender[student@desktopX ~]$
pass<Tab>
[student@desktopX ~]$
passwd
Changing password for user student. Changing password for student. (current) UNIX password:
Arguments and options can be matched with tab completion for many commands. The useradd command is used by the superuser,[student@desktopX ~]$
ls /etc/pas<Tab>
[student@desktopX ~]$
ls /etc/passwd<Tab>
passwd passwd-
root
, to create additional users on the
system. It has many options that can be used to control how that
command behaves. Tab completion following a partial option can be
used to complete the option without a lot of typing.
[root@desktopX ~]#
useradd --<Tab><Tab>
--base-dir --groups --no-log-init --shell --comment --help --non-unique --skel --create-home --home-dir --no-user-group --system --defaults --inactive --password --uid --expiredate --key --root --user-group --gid --no-create-home --selinux-user[root@desktopX ~]#
useradd --
Command History
The history command displays a list of previously
executed commands prefixed with a command number.
The exclamation point character, !, is a metacharacter that is used to expand previous commands without having to retype them. !
The Esc+. key combination causes the shell to copy the last word of the previous command on the current command line where the cursor is. If used repeatedly, it will continue to go through earlier commands.
The exclamation point character, !, is a metacharacter that is used to expand previous commands without having to retype them. !
number
expands to the command matching the number specified.
!string
expands to
the most recent command that begins with the string specified.
The arrow keys can be used to navigate through previous command lines in the shell's history. Up Arrow edits the previous command in the history list. Down Arrow edits the next command in the history list. Use this key when the Up Arrow has been pressed too many times. Left Arrow and Right Arrow move the cursor left and right in the current command line being edited.[student@desktopX ~]$
history
...Output omitted... 23 clear 24 who 25 pwd 26 ls /etc 27 uptime 28 ls -l 29 date 30 history[student@desktopX ~]$
!ls
ls -l total 0 drwxr-xr-x. 2 student student 6 Mar 29 21:16 Desktop ...Output omitted...[student@desktopX ~]$
!26
ls /etc abrt hosts pulse adjtime hosts.allow purple aliases hosts.deny qemu-ga ...Output omitted...
The Esc+. key combination causes the shell to copy the last word of the previous command on the current command line where the cursor is. If used repeatedly, it will continue to go through earlier commands.
Editing the Command Line
When used interactively, bash has a command line-editing feature.
This allows the user to use text editor commands to move around within
and modify the current command being typed. Using the arrow keys to move
within the current command and to step through the command history was
introduced earlier in this session. More powerful editing commands
are introduced in the following table.
There are several other command line-editing commands available,
but these are the most useful commands for beginning users. The other
commands can be found in the bash(1) man page.
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+Left Arrow | Jump to the beginning of the previous word on the command line. |
Ctrl+Right Arrow | Jump to the end of the next word on the command line. |
Ctrl+r | Search the history list of commands for a pattern. |
Getting Help with Command Syntax
To use a command effectively, a user needs to know what options
and arguments it takes and in what order it expects them (the
syntax of the command). Most commands have a
--help option. This causes the command to print
a description of what it does, a "usage statement" that describes
the command's syntax, and a list of the options it accepts and what
they do.
Usage statements may seem complicated and difficult to read. They become much simpler to understand once a user becomes familiar with a few basic conventions:
Consider the first usage statement for the date command:
System Documentation
The References boxes throughout this course refer to two additional resources on the server that can be used to get information about commands: man pages and Info nodes.
Manual pages, or man pages, consist of system documentation which may be installed that provides detailed information about specific kinds of topics. The manual is divided into sections numbered from 1 through 9, and each section is for a specific kind of information. For example, section 1 covers user programs; section 8 covers administratives programs. Man pages are referred to by their name, followed by the number of the relevant section in parentheses: bash(1) is the man page for the bash command in section 1 of the manual.
To read a man page, enter the man command, followed optionally by the section of the manual for the man page and the name of the page itself.
Info provides hypertext documents made up of multiple pages of information, or nodes. Unlike a man page, which are free-standing and focus on a particular command, file, or function, an Info node is usually one part of a larger document on a topic. To access Info nodes, run the info command. It may be followed by arguments that will be interpreted first as the name of the Info document to open, or if that doesn't resolve, to the name of a particular Info node in a document. If no arguments are given, a master directory of Info documents is opened.
The basic commands to read an Info node with info are similar to those for man: scroll through the node a screen at a time with the Space bar, move up and down a line at a time with arrow keys, and quit by pressing q. Other keys allow you to go to other Info nodes: n to the next node, p to go to the previous node, and place the cursor on a hyperlink and press Enter to follow the hyperlink to another node. More usage information is available by running info info.
You do not need to be able to read any man pages or Info nodes in order to complete this course. The References are provided for additional background information.
info info (Stand-alone GNU Info)
Usage statements may seem complicated and difficult to read. They become much simpler to understand once a user becomes familiar with a few basic conventions:
-
Square brackets, [], surround optional items.
-
Anything followed by ... represents an
arbitrary-length list of items of that type.
-
Multiple items separated by pipes, |,
means only one of them can be specified.
-
Text in angle brackets, <>, represents
variable data. For example, <filename> means
“insert the filename you wish to use hereâ€.
Sometimes these variables are simply written in capital letters
(e.g., FILENAME).
Consider the first usage statement for the date command:
[student@desktopX ~]$
date --help
date [OPTION]... [+FORMAT]
This indicates that date can take an optional list of options ([OPTION]...), followed by an optional format string, prefixed with a plus character, +, that defines how the current date should be displayed ([+FORMAT]). Since both of these are optional, date will work even if it is not given options or arguments (it will print the current date and time using its default format).
System Documentation
The References boxes throughout this course refer to two additional resources on the server that can be used to get information about commands: man pages and Info nodes.
Manual pages, or man pages, consist of system documentation which may be installed that provides detailed information about specific kinds of topics. The manual is divided into sections numbered from 1 through 9, and each section is for a specific kind of information. For example, section 1 covers user programs; section 8 covers administratives programs. Man pages are referred to by their name, followed by the number of the relevant section in parentheses: bash(1) is the man page for the bash command in section 1 of the manual.
To read a man page, enter the man command, followed optionally by the section of the manual for the man page and the name of the page itself.
You can scroll through the document a screen at a time with the Space bar, move up and down a line at a time with arrow keys, and quit by pressing q. The man page for a command will generally start with a discussion of the command's syntax, and then discuss the various options and arguments in great detail.[student@desktopX ~]$
man 1 bash
Info provides hypertext documents made up of multiple pages of information, or nodes. Unlike a man page, which are free-standing and focus on a particular command, file, or function, an Info node is usually one part of a larger document on a topic. To access Info nodes, run the info command. It may be followed by arguments that will be interpreted first as the name of the Info document to open, or if that doesn't resolve, to the name of a particular Info node in a document. If no arguments are given, a master directory of Info documents is opened.
The basic commands to read an Info node with info are similar to those for man: scroll through the node a screen at a time with the Space bar, move up and down a line at a time with arrow keys, and quit by pressing q. Other keys allow you to go to other Info nodes: n to the next node, p to go to the previous node, and place the cursor on a hyperlink and press Enter to follow the hyperlink to another node. More usage information is available by running info info.
Important
It is likely that not all man pages or Info nodes discussed in this course will be installed on any given system, including the system used for exercises. Which ones are present depends on the software packages which have been installed.You do not need to be able to read any man pages or Info nodes in order to complete this course. The References are provided for additional background information.
References
bash(1), date(1), file(1), head(1), info(1), man(1), passwd(1), tail(1), and wc(1) man pagesinfo info (Stand-alone GNU Info)
Comments
Post a Comment
thank you for visiting :)