Linux commonly used special symbols

The special symbols commonly used in the shell are listed as follows:

$ ${} $? $$ $*

$# $@ command{} [] [[]] () (())

  • ; Command separator

    In the shell,; is mainly used to run one or more instructions on a line.

  • ;; The consecutive semicolon (Terminator) is
    dedicated to the case option and serves as the role of Terminator.
    case “$fop” inhelp) echo “Usage: Command -help -version filename”;;version) echo “version 0.1” ;;esac

  • 'string' single quote "string" Double quotes (double quote)
    enclosed in single quotes will be treated as a single string. The $ symbol inside the quotation marks on behalf of the variable has no effect, that is, it is treated as a general symbol to prevent any variable substitution. The content enclosed in double quotes will be treated as a single string. It prevents wildcard expansion, but allows variable expansion. This is different from the processing of single argument.

[root@localhost ~]# vartest=1234
[root@localhost ~]# echo '$vartest is good'
$vartest is good
[root@localhost ~]# echo $vartest
1234
[root@localhost ~]# echo "$vartest is good"
1234 is good
  • command Backticks Use backticks to execute instructions.
[root@localhost ~]# fdv=`ls`
[root@localhost ~]# echo $fdv
anaconda-ks.cfg Desktop Documents Downloads initial-setup-ks.cfg Music original-ks.cfg Pictures Public Templates Video
  • | Pipeline

Pipeline is a basic and important concept of UNIX system. Connect the standard output of the previous command as the standard input of the next command.

[root@localhost ~]# who |wc -l
5
  • $ Related instructions

$ (dollar sign)
represents the symbol of Variable Substitution.
$*: Refers to the execution reference variable of the script. The algorithm for referencing the parameters is the same as the general instruction. The instruction itself is 0, followed by 1, and so on.
$@ and $* have the same symbol, but there is one difference between them.
The symbol $* treats all reference variables as a whole. But the symbol $@ still retains the section concept of each reference variable.
$#: This is also a symbol related to reference variables. Her role is to tell you what the total number of reference variables is.
$? :Status variable
Generally speaking, the process of UNIX(linux) system ends by executing the system call exit(). This return value is the status value. Return to the parent process to check the execution status of the child process. If the general instruction program is executed successfully, the return value is 0; if it fails, it is 1.

  • Parentheses ()
    ①Command group. The command in the brackets opens a new subshell program. The variables in the brackets are local variables and cannot be used in other parts of the script. Multiple commands in brackets are separated by semicolons.
    ②Command replacement. The command substitution $(cmd) is equivalent to cmd(this is not a single quote, `is the key under ESC). If the $(cmd) structure is found during shell execution, the cmd in $(cmd) will be executed once to get its output , And put this output in the original command. E.g:
[root@localhost tmp]# ls
fstab  functions  hellobash  issue  mytestdir  scripts
[root@localhost tmp]# echo $(ls)
fstab functions hellobash issue mytestdir scripts
[root@localhost tmp]# echo `ls`
fstab functions hellobash issue mytestdir scripts
[root@localhost tmp]# 
  • {} Braces (Block of code)

①Expand. Expand the file name in the braces. In the braces, no white space is allowed unless the white space is quoted or escaped. Expansion is divided into ordinary expansion with a comma (,), such as echo {a,b}.txt lists all the contents of the interval; expansion has been carried out with two dots (...), such as echo {1...5}.txt Automatically complete the middle content from 1 to 5.

[root@localhost ~]# echo {a,b}.txt
a.txt b.txt
[root@localhost ~]# echo {1..5}.txt
1.txt 2.txt 3.txt 4.txt 5.txt
[root@localhost ~]# 

②Internal group. Unlike the commands in parentheses, the commands in the curly brackets are executed in the current shell and will not reopen the subshell. The commands in parentheses are separated by semicolons, and the last command must be followed by a semicolon. There must be a space between the first command of {} and the opening bracket.

  • [] Brackets

It often appears in process control, playing the role of enclosing judgment.
[[ ]]
This group of symbols is basically the same as the previous [] symbols, but it allows the direct use of || and && logic symbols in it.

#!/bin/bash
read ak
if [[ $ak > 5 || $ak< 9 ]]
then
echo $ak
fi
  • & Background work

A single ampersand and put it at the end of the complete command line means that the command line is put into the background to work.
tar cvfz data.tar.gz data> /dev/null&

Guess you like

Origin blog.csdn.net/m0_52425873/article/details/113049103