First meeting shell


Meet shell

What is shell

  • Shell is a command parser, which is used to parse the user’s operation on the operating system
  • Shell has a lot
    • cat /etc/shells
  • The shell used by CentOS 7 by default is bash

Linux startup process

  • BIOS → MBR → bootloader (grub) → kernel → system D → system initialization → shell

Shell script

  • UNIX philosophy: one command does only one thing
  • In order to combine commands and multiple executions, a script file is used to save the commands that need to be executed
  • Give the file execution permission (CHMOD U + RX filename)

Standard shell scripts contain elements

  • Sha-Bang
  • command
  • Comments beginning with “#”
  • CHMOD U + RX filename executable permissions

Impact of different execution methods of scripts

  • Bash. / (there is no need to add execution permission, do not change the current directory, and generate child processes)
  • . / (you need to add execution permission without changing the current directory to generate child processes)
  • Source. / (you need to add execution permission, change the current directory, and no child processes are generated)
  • . (you need to add execution permission, change the current directory, and no child process is generated)

Differences between built-in commands and external commands

  • Built in commands do not require the creation of child processes
  • The built-in command takes effect on the current shell

Pipe to pipe

  • Pipeline, like signal, is also one of the ways of process communication
  • Anonymous pipeline (pipeline symbol) is a communication tool often used in shell programming
  • The pipe character is “|” which passes the execution result of the previous command to the following command
ps | cat
echo 123 | ps

Redirection symbol

  • A process will open three file descriptors: standard input, standard output and error output by default
  • Enter redirection symbol “<“
read var < /path/to/a/file
  • Output redirection symbol
    • “>”: empty the content and then output
    • “> >”: newline append output
    • “2 >”: error output
    • “& >”: output all
echo 123 > /path/to/a/file
  • Input and output redirection are used in combination
cat > /path/to/a/file << EOF
I am $USER

Definition of variables

  • Naming rules for variable names
    • Letters, numbers, underscores
    • Do not start with a number

Assignment of variables

The process of assigning values to variables is called variable substitution

  • Variable name = variable valuea=123
  • Assign values to variables using letlet a=10 + 20
  • Assign a command to a variablel=ls
  • Assign the command result to a variable, using $() or`letc=$(ls -l /etc)`
  • Variable values have special characters such as spaces, which can be included in ” or ”

No spaces allowed

Reference to variable

  • ${variable name}It’s called a reference to a variable
  • Echo ${variable name}View the value of the variable
  • ${variable name}In some cases, it can be omitted as$variable name

Scope of variable

  • Export of variables
    • export
  • Deletion of variables
    • unset

System environment variable

  • Environment variable: a variable that can be obtained when each shell is opened

    • Set and env commands
    • $? $$ $0
    • $PATH
    • $PS1
  • Position variable

    • $1 $2 … $n

Environment variable profile

  • /etc/profile
  • /etc/profile.d/
  • ~/.bash_profile
  • ~/.bashrc
  • /etc/bashrc

This work adoptsCC agreement, reprint must indicate the author and the link to this article