Linux Learning 1 - Bash Shell Basic

Made by Mike_Zhang


Unfold Linux Topics | 展开Linux主题 >


1. Introduction to Linux

Shell is an interface between the user and the core components of the operating system, known as the kernel.

  • The most significant component of the operating system is the kernel. This program contains the components of the operating system that will stay resident in memory. The kernel handles such tasks as process management, resource management, memory management, and input/output operations.

Linux shell itself contains the CLI, an interpreter, and an environment of previously defined entities like functions and variables.

  • The interpreter is a program which accepts user input, interprets the command entered, and executes it.

2. Bash Shell

BASH / Bash / Bash shell: Bourne-again Shell

  • the default shell for most Linux users.
1
[user@localhost ~]$
  • user: the user name;
  • localhost: machine you are on, localhost means that the shell you have opened is on this computer;
  • ~: the current working directory, ~ means user’s home directory.
  • $: the prompt, precede any input you type.
    • $: a normal user;
    • #: have logged in to this shell as root (the system administrator).

2.1 Basic Commands

Command Description
whoami output the user’s username
who list the users currently logged in to the computer you are operating on, including their terminal (which terminal window they have opened or whether they are on console).
pwd output the current working directory
hostname output the name of the host (this will either be localhost.localdomain or the IP address or IP alias of the machine logged in to)
ls list the contents of the current working directory
passwd used to change your password. You are first prompted to input your current password and then your new password (twice). If the password you enter is not sufficiently strong, you are warned.
uname output basic information about your operating system.
arch output basic information about your computer’s hardware (architecture).
bash start a new Bash shell (if in a shell, this starts a new session so that the outer shell session is hidden). A variation is sh, to start a new shell. The sh program may not start a Bash shell however depending on which version of Linux you are using.
exit leave the current Bash shell and if this is the outermost Bash shell, close the window.

Can also enter multiple commands from the prompt by separating each with a semicolon:

1
2
3
4
5
[user@localhost ~]$ whoami; pwd; ls
physics
/Users/physics
Applications Documents
...

2.2 Commands Options and Parameters

Most Linux commands expect arguments. Arguments come in two basic forms, options and parameters.

  • Options: user-specified variations to the command so that the command performs differently.
  • Parameters: commonly either file names or directories (or both).

Format:

1
command [option(s)] [parameter(s)]

For ls command:

  • Lists the current directory’s contents. Typing ls will display for you the visible files and subdirectories in the current directory. The term visible indicates entries whose names do NOT start with a period (.).
1
2
3
[user@localhost ~]$ ls
$RECYCLE.BIN FWL MI
...

ls -l: provides a “long” listing of the e ties. That is, it provides more details than just the name of each item.

1
2
3
4
5
6
[user@localhost ~]$ ls -l
total 8
drwxr-xr-x 3 physics staff 96 Jun 19 2022 $RECYCLE.BIN
drwxr-xr-x@ 12 physics staff 384 Dec 16 13:35 DBS
drwxr-xr-x@ 9 physics staff 288 Nov 28 17:18 FWL
...

ls [file name]: to see the long listing of a single file called [file name]

1
2
3
4
5
6
[user@localhost ~]$ ls -l MI
total 208
drwxr-xr-x@ 8 physics staff 256 Nov 29 15:26 Exam
drwxr-xr-x@ 6 physics staff 192 Sep 9 14:46 Exercises
drwxr-xr-x@ 5 physics staff 160 Nov 22 14:04 Homework
...

ls command options

Option Meaning
-a “List all” entries, not only list the hidden, or dot, files (those that start with a dot), it will also list . and ..
-A Same as -a except that . and .. are not shown
-B Ignore items whose names end with ~ (~is used for backup files)
-C List entries in columns (fits more items on the screen)
-d List directories by name, do not list their content
-F Append listings with item classifications (ends directory names with/, ends executable files with *)
-l provides a “long” listing of the e ties. That is, it provides more details than just the name of each item
-g Same as –l except that owner is not shown
-G Same as –l except that group owner is not shown
-h When used with –l, modifies file sizes to be “human readable”
-i Include inode number of items
-L Dereference links—that is, display information about item being linked to, not the link itself
-r List items in reverse alphabetical order
-R Recursive listing (list contents of all subdirectories)
-s When used with –l, outputs sizes in blocks rather than bytes
-S Sort files by size rather than name
-t Sort files by modification time rather than name
-X Sort files by extension name rather than name
-1 (numeric 1) List files one per line (do not use columns)

2.3 Man Page

man

  • short for manual page. This command displays the manual contents for a given Linux command.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[user@localhost ~]$ man ls
LS(1) General Commands Manual LS(1)

NAME
ls - list directory contents

SYNOPSIS
ls [-@ABCFGHILOPRSTUWabcdefghiklmnopqrstuvwxy1%,] [--color=when] [-D format] [file ...]

DESCRIPTION
For each operand that names a file of a type other than directory, ls displays its name as well as any requested, associated information. For each operand that names a file of type directory, ls displays the
names of files contained within that directory, as well as any requested, associated information.

If no operands are given, the contents of the current directory are displayed. If more than one operand is given, non-directory operands are displayed first; directory and non-directory operands are sorted
separately and in lexicographical order.

The following options are available:

-@ Display extended attribute keys and sizes in long (-l) output.
:

Man Page Contents


After each screen, the man page pauses with the “:” prompt. At this prompt, man awaits your keystroke to command how man should continue. You have the following options:

  • Forward one screen—‘f,’ ‘z,’ space bar
  • Forward half a screen—‘d’
  • Forward one line—‘e,’ ‘j,’ enter key, down arrow
  • Back one screen—‘b,’ ‘w’
  • Back one half screen—‘u’
  • Back one line—‘b,’ up arrow
  • Return to top of man page—‘1G’
  • Go to bottom of man page—‘G’
  • Go to line #—#G (# is a number like 10G for line 10)
  • Obtain help—‘h,’ ‘H’
  • To move forward # lines—# (# is a number)
  • To search forward for a string—/string
  • To search backward for a string—?string
  • Exit man page—‘q,’ ‘Q’

2.4 Commands History

history command:

  • provide you a list of every command that has been entered.
1
2
3
4
5
6
7
8
9
[user@localhost ~]$ history
...
510 ls -al
511 ls -a
512 ls -l
513 ls -l PolyU
514 ls -l MI
515 man ls
516 history

History recall command

Command Meaning
!# Recall the instruction from line # in the history list;
!! Recall the last instruction from the history list;
!string Where string is a string of character to recall the most recent instruction that started with the given string

To step through the history list from the command line:

  • pressing the up arrow key;
  • pressing control+p key;

history command options

Option Meaning
-c Clear the history list
-d # Where # is a number, delete that numbered entry from the history list
-w Write the current history list to the history file
-r Input the history file and use it instead of the current history
# Where # is a number, display only the last # items in the list

2.5 Command Line Editing

To support the user in entering commands, the Bash interpreter accepts a number of special keystrokes that, when entered, move the cursor, copy, cut, or paste characters.

(c for control key, m for escape key)

Keystroke Meaning
c+a Move cursor to beginning of line
c+e Move cursor to end of line
c+n (also up arrow) Move to next instruction in history list
c+p (also down arrow) Move to previous instruction in history list
c+f (also right arrow) Move cursor one character to the right
c+b (also left arrow) Move cursor one character to the left
c+d (also delete key) Delete character at cursor
c+k Delete all characters from cursor to end of line
c+u Delete everything from the command line
c+w Delete all characters from front of word to cursor
c+y Yank, return all deleted characters (aside from c+d) to cursor position
c+_ Undo last keystroke
m+f Move cursor to space after current word
m+b Move cursor to beginning of current word
m+d Delete the remainder of the word

2.6 Redirection

The forms of redirection

Command Meaning
> Send output to the file whose name is specified after the >; if the file already exists, delete its contents before writing.
>> Append output to the file whose name is specified after the >>; if the file does not currently exist, create it.
< Use the file whose name is specified after the < as input to the command.
<< Use STDIN (keyboard) as input. As the key can be part of any input entered, we need to denote the end of input. There are two mechanisms for this. The user can either use control+d to indicate the end of input or place a string after the << characters to be used as an “end of input” string.
Redirect the output of one command to serve as the input to the next command.

[Example]

cat<<[string]:

  • To establish the end of the input;
  • This means that as the user enters strings and presses , each string is sent to cat. However, if the strings matches our end-of-input [string], then cat will terminate.
1
2
3
4
5
6
7
8
9
10
11
$ cat << done
> pear
> banana
> cherry
> strawberry
> done
pear
banana
cherry
strawberry
$

Can also save the input to a file:

1
2
cat << done > fruit
...

References

R. Fox, Linux with operating system concepts. Boca Raton, Fl: Crc Press, 2015.


Outro

尾巴
本文只覆盖了Linux中Bash的基础部分,马上继续更新。
最后,希望大家一起交流,分享,指出问题,谢谢!


原创文章,转载请标明出处
Made by Mike_Zhang




感谢你的支持 | Thank you for supporting

Linux Learning 1 - Bash Shell Basic
https://ultrafish.io/post/linux-learning-1/
Author
Mike_Zhang
Posted on
December 21, 2022
Licensed under