Summary of basic usage of $ symbol in Linux

Summary of basic usage of $ symbol in Linux

Linux version: CentOS 7

[root@azfdbdfsdf230lqdg1ba91 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@azfdbdfsdf230lqdg1ba91 ~]# uname -a
Linux azfdbdfsdf230lqdg1ba91 3.10.0-693.2.2.el7.x86_64 #1 SMP Tue Sep 12 22:26:13 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@azfdbdfsdf230lqdg1ba91 ~]#

$ symbol grass set

Target

The $ symbol plays many important roles in the Linux system, especially when writing bash scripts, where $ can be seen everywhere. Due to its ever-changing and diverse nature, it poses a challenge to master and use it, especially to remember it. So now, let us summarize its usage and form a collection. Mastering them won't give you a big salary increase, because they won't be asked in interviews, but it will improve your work efficiency and expand your horizons.

At present, the uses of $ that I know include $, "$", $0 $1 $n, $#, $@ $*, $?, $(), ${}, ${#}, $[], $-, $!, and $$. Extra !$, !!, detailed description in turn

$ Get variable value

$ can get the value of the variable

[root@izbp10lqlgy2g31s41bt94z ~]# a=1
[root@izbp10lqlgy2g31s41bt94z ~]# echo $a
1

It is best to use "$" to get the value of a variable.

Why is there this suggestion? See the example

[root@izbp10lqlgy2g31s41bt94z ~]# echo get value of a = $a
get value of a = 1
[root@izbp10lqlgy2g31s41bt94z ~]# echo "get value of a = $a"
get value of a = 1

As you can see, the effect seems to be the same whether there are double quotes or not. Don't jump to conclusions yet, read on.

[root@izbp10lqlgy2g31s41bt94z ~]# a="i am skyler"
[root@izbp10lqlgy2g31s41bt94z ~]# [ $a == "i am skyler" ]
-bash: [: too many arguments

Here, [] is a conditional judgment symbol, which is equivalent to the test command. He means to determine whether the value of variable a is equal to "i am skyler".
So why does it report an error? Because the variable written in this way [ $a == "i am skyler"] becomes [ i am skyler == "i am skyler" ] after parsing. Obviously, this judgment expression cannot judge the strings on both sides of the equal sign. What we want is the comparison [ "i am skyler" == "i am skyler" ]. So usually we are name"

[root@izbp10lqlgy2g31s41bt94z ~]# [ "$a" == "i am skyler" ]
[root@izbp10lqlgy2g31s41bt94z ~]# echo $?
0

Just use double quotes. Here, $? is used in advance, which means to determine whether the execution result of the previous command is correct. In the output result, 0 indicates successful execution, and non-zero value indicates an error.

${} is used to distinguish the boundaries of variables and clearly tell the program which variable value to take

In the following example, without adding {}, the program cannot determine which of ab $abc is a variable and cannot parse

[root@izbp10lqlgy2g31s41bt94z ~]# echo "get value of a = $abc"
get value of a =
[root@izbp10lqlgy2g31s41bt94z ~]# echo "get value of a = ${a}bc"
get value of a = 1bc
[root@izbp10lqlgy2g31s41bt94z ~]#

${#}Get the length of the variable value

[root@izbp10lqlgy2g31s41bt94z ~]# echo "get length of a = ${#a}"
get length of a = 1
[root@izbp10lqlgy2g31s41bt94z ~]# a=11111
[root@izbp10lqlgy2g31s41bt94z ~]# echo "get length of a = ${#a}"
get length of a = 5
[root@izbp10lqlgy2g31s41bt94z ~]# a=skyler
[root@izbp10lqlgy2g31s41bt94z ~]# echo "get length of a = ${#a}"
get length of a = 6
[root@izbp10lqlgy2g31s41bt94z ~]#

$0 $1 $n Get the file name and parameter value, usually used in bash scripts

$0 represents the shell script file name; starting from 1, it represents the number of parameters, and 1 represents the first parameter. Here we create a test.sh executable file

Create a test.sh file and fill it with code [root@izbp10lqlgy2g31s41bt94z ~]# echo 'echo $0 $1 $2' > test.sh
[root@izbp10lqlgy2g31s41bt94z ~]# cat test.sh
echo $0 $1 $2

Execute test.sh and pass in the variable [root@izbp10lqlgy2g31s41bt94z ~]# sh test.sh i am skyler
test.sh i am

As you can see, the first two of the three parameters are printed out. Since we did not declare $3, the file name and the first two parameters are printed out.

$# Get the number of parameters

[root@izbp10lqlgy2g31s41bt94z ~]# echo 'echo $# $0 $1' > test.sh
[root@izbp10lqlgy2g31s41bt94z ~]# cat test.sh
echo $# $0 $1
[root@izbp10lqlgy2g31s41bt94z ~]# sh test.sh I am a shuashua
4 test.sh I

$@ $* array-style reference parameter list

The difference between them is that when double quotes are used, assuming the parameter passed in is 1 2 3, then the value of "*" is "1 2 3" a variable

test.sh
echo '$@ array parameter format'
for x in "$@"
do
 echo + $x
done
echo '$* array parameter format'
for x in "$*"
do
 echo + $x
done

root@izbp10lqlgy2g31s41bt94z:~# sh test.sh 1 2 3
Array parameter format of $@+ 1
+ 2
+ 3
$* array parameter format + 1 2 3

$? Determine whether the previous command was executed successfully

The execution success value is 0, and failure is non-zero.

[root@izbp10lqlgy2g31s41bt94z ~]# ll
Total dosage 172
-rw-r--r-- 1 root root 49392 February 25, 2019 hs_err_pid24203.log
-rw-r--r-- 1 root root 49425 February 13, 2019 hs_err_pid25726.log
[root@izbp10lqlgy2g31s41bt94z ~]# echo $?
0
[root@izbp10lqlgy2g31s41bt94z ~]# ca ff
-bash: ca: command not found [root@izbp10lqlgy2g31s41bt94z ~]# echo $?
127

$() is equivalent to using double quotes

slightly

$[] Expression evaluation

At this time, [] is not used for judgment scenarios. [] is generally used as a judgment in conditional statements such as if while in bash scripts.

[root@izbp10lqlgy2g31s41bt94z ~]# echo $[5 + 5]
10

$ - displays the current options used by the shell

[root@izbp10lqlgy2g31s41bt94z ~]# echo $-
himBH

Explanation: Each character in himBH is a shell option. For details, go to man bash and search for -h -B etc. For more information, please refer to: http://kodango.com/explain-shell-default-options

$! Get the pid of the last process running in the background. More applications in bash scripts

[root@izbp10lqlgy2g31s41bt94z ~]# cat test.sh &
[1] 362
[root@izbp10lqlgy2g31s41bt94z ~]# echo $# $0 $1
^C
[1]+ Complete cat test.sh
[root@izbp10lqlgy2g31s41bt94z ~]# echo $!
362

!$ passes the parameters of the previous command to the parameters of the next command. It is usually more convenient and is more used in bash scripts.

[root@izbp10lqlgy2g31s41bt94z ~]# cd /Users/skyler/project/test
[root@izbp10lqlgy2g31s41bt94z ~]# ll !$
[root@izbp10lqlgy2g31s41bt94z ~]# ll /Users/skyler/project/test
362

!! Output the previous command, usually more convenient, more used in bash scripts

[root@izbp10lqlgy2g31s41bt94z ~]# !!
[root@izbp10lqlgy2g31s41bt94z ~]# ll /Users/skyler/project/test

$$ Get the current process pid

[root@izbp10lqlgy2g31s41bt94z ~]# echo $$
31268
[root@izbp10lqlgy2g31s41bt94z ~]# ps -ef|grep 31268
root 31268 31266 0 08:10 pts/0 00:00:00 -bash

The current process is bash, pid is 31268

Practice more often on weekdays and ask for it when you need it

The effect can also be achieved by paging and pulling segments, but the specific effect depends on the business scenario.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • A brief explanation of the meaning of shell variables $#, $@, $0, $1, $2 in Linux
  • The difference between php $_SERVER in Windows and Linux
  • Solve the problem of ssh remote login to linux showing -bash-4.1$
  • How to change $ to # in Linux

<<:  MySQL 8.0.15 installation and configuration graphic tutorial and password change under Linux

>>:  JavaScript macrotasks and microtasks

Recommend

Teach you a trick to achieve text comparison in Linux

Preface In the process of writing code, we will i...

SQL Practice Exercise: Online Mall Database Product Category Data Operation

Online shopping mall database-product category da...

How to connect Navicat to the docker database on the server

Start the mysql container in docekr Use command: ...

JavaScript Design Pattern Command Pattern

The command pattern is a behavioral design patter...

Graphical tutorial on installing JDK1.8 under CentOS7.4

Linux installation JDK1.8 steps 1. Check whether ...

How to solve the Docker container startup failure

Question: After the computer restarts, the mysql ...

Solve the problem of secure_file_priv null

Add secure_file_priv = ' '; then run cmd ...

Detailed explanation of JS ES6 variable destructuring assignment

Table of contents 1. What is deconstruction? 2. A...

Implementing a simple web clock with JavaScript

Use JavaScript to implement a web page clock. The...

SQL implementation LeetCode (176. Second highest salary)

[LeetCode] 176. Second Highest Salary Write a SQL...

MySQL 5.7 installation and configuration tutorial

This article shares the MySQL installation and co...