“This is the 13th day of my participation in the Gengwen Challenge. For more details, see” Gengwen Challenge “.

In this paper, we introduce the basis of the shell is used, including some basic concepts (such as environment variables, flow, path) is introduced and some commands (such as: echo CD | | ls) introduction to the use.

I met the shell,echo

In the terminal, output the current date:

ShellLearn :~$date Sunday, June 13, 2021 09:47:36 CSTCopy the code

The echo command takes one argument and outputs hello to the terminal with the input.

shellLearn:~$ echo hello
hello
Copy the code

What if the input contains a special string or a space?

shellLearn:~$ echo 'hello world'
hello world
shellLearn:~$ echo "hello world"
hello world
shellLearn:~$ echo hello\ world
hello world
Copy the code

Solution:

  • Use single quotes, double quotes around it
  • Use escape symbols\For processing

Environment variable $PATH

How does the shell know where to look for date or Echo?

In fact, like Python or Node, the shell is a programming environment. When you execute commands in the shell, you are actually executing a short piece of code that the shell can interpret and execute.

If you ask the shell to execute a command that is not a programming keyword known to the shell, it will consult the environment variable $PATH, which lists the PATH to search the program when the shell receives the command:

shellLearn:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Copy the code

As you can see, the output of $PATH is a patchwork of directories and:.

  • /usr/local/sbin
  • /usr/local/bin
  • /usr/sbin
  • /usr/bin
  • /sbin
  • /bin

When we run echo, the shell learns that we need to run echo, and then it searches the $PATH for a list of directories split by:, searches for the program by name, and executes it when it finds it.

To determine which specific program a program name represents, you can use the which program.

Once the specific program is identified, you can bypass the environment variables and directly specify the path to the program that you want to execute.

shellLearn:~$ which echo
/bin/echo
shellLearn:~$ /bin/echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Copy the code

Obtaining the Help Manual

What if you are unfamiliar with a command such as XYZ and want to ask for help? There are two ways:

  • throughman xyz

The following operations will obtain the learning manual of the CD command:

shellLearn:~$ man cd.Copy the code

You can press Q to exit the manual.

  • throughabc -habc --help

In general, use the -h or –help tag when executing a program to print help information to see what tags or options are available.

Note, however, that not all commands support this usage (the CD command on the Mac does not support -h).

The following command will show how mv works:

shellLearn:~$ mv -h
...
Copy the code

Navigation Operations (path switching)

In the process of our work, we often need to change the current working path. So how do you switch paths.

Path separator

A path in the shell is a set of directories that are split, using/on Linux and macOS and \ on Windows. Something like this:

  • macOS : /Users/xxxx/shellLearn
  • Windows:C:\Users\shellLearn

The root directory

The/path represents the root directory of the system. All folders are included under this path. On Windows, each disk has a root directory (e.g., C:\).

Absolute path, relative path

* In nux, if a path starts with /, it is an absolute path and everything else is relative. The relative path is the path relative to the current working directory. In relative paths,. Represents the current directory, while.. Indicates the parent directory.

Path jump:pwd,cdThe command

  • The PWD command is used to view the current working directory.
  • The CD command is used to switch directories.
shellLearn:/home$ pwd
/home
shellLearn:/home$ cd shellLearn
shellLearn:~$ pwd
/home/shellLearn
Copy the code

cd -The command returns to the previous working directory

File directory Operation

List the files in the directory:ls

To see what files are contained in a specified directory, we use the ls command. If you do not specify a directory for ls, it will print files in the current directory by default.

shellLearn:/home$ ls
shellLearn
Copy the code

A few other common operations

  • Mv (used to rename or move files)
  • Cp (copy files)
  • Mkdir (New folder)

Flow operation

In the shell, programs have two main “streams” : their input stream and their output stream.

When programs try to read information, they read it from the input stream, and when programs print information, they print it to the output stream.

Typically, the input and output streams of a program are your terminals. That is, your keyboard serves as input and your monitor serves as output. However, we can redirect these streams as well!

We mainly learn four operation symbols:

  • Redirect streams < file and > file

These two commands redirect the input and output streams of a program to a file, respectively

shellLearn:~$ echo hello > hello.txt
shellLearn:~$ cat hello.txt
hello
shellLearn:~$ cat < hello.txt
hello
shellLearn:~$ cat < hello.txt > hello2.txt
shellLearn:~$ cat hello2.txt
hello
Copy the code
  • Additional content>> file

You can append content to a file using >>

  • The pipe|The operator

Using pipes, we can make better use of file redirection. | operator allows us to a program’s output and the other a program’s input link:

shellLearn:~$ ls -l / | tail -n1
lrwxr-xr-x@  1 root  admin    11  6  4  2020 var -> private/var
shellLearn:~$ curl --head --silent qq.com | grep --ignore-case content-length
Content-Length: 169
Copy the code

Note: tail-nn, where ‘N’ is a number, specifies how many lines are printed from the previous stream, starting with the reciprocal.

Summary and Acknowledgements

This paper mainly introduces the principle of shell executing commands, that is, finding the corresponding program from environment variables and executing it. On this basis, we learn several kinds of operations, such as file directory operation, navigation operation, flow operation and help operation.

Although I have used shell for many years in my daily work, I have never systematically learned it, so I just take this opportunity to comb and learn.

This article is The study notes of The online open course “The Missing Semester of Your CS Education”. In The synchronous learning process, I have gained a lot. Thanks and respect.