Shell special variables in Linux $ $n $* $@ $! The detailed explanation of the $?

Source: Internet
Author: User

$: Gets the file name of the currently executing script, including the path.

[Root@test script]# Cat 0.sh
#!/bin/bash
echo $
[Root@test script]# SH 0.sh
0.sh
[Root@test script]# Cat 0.sh
#!/bin/bash
DirName "$"
BaseName "$"
[Root@test script]# sh/byrd/script/0.sh
/byrd/script
0.sh

$n: Gets The nth parameter of the current row's shell script, n=1..9, when n is 0, the file name of the script, if n is greater than 9, enclose the like${10} with braces.

[Root@test script]# Cat n.sh
#!/bin/bash
echo $ $ ${10}
[Root@test script]# sh n.sh a b c d e F g h i j k l m N
A B J
[Root@lamp script]# sh n.sh {a.. Z
A B J
[Root@test script]# sh n.sh ' seq 11 '
1 2 10

$*: Gets all the parameters of the current shell and treats all command-line arguments as a single string.
$@: All parameters of this program "$" "$" "$", which is the best way to pass parameters to other programs, so TA retains any whitespace embedded in each parameter.
$#: Gets the total number of arguments in the current shell command line.

[Root@test script]# cat hashtag.sh
#!/bin/bash
echo "$#"
[root@test script]# sh hashtag.sh
0
[Roo T@test script]# sh hashtag.sh 1 2 3
3
[root@test script]# sh hashtag.sh ' seq '
#
[Root@test script]# Cat example.sh
#!/bin/bash
#Example
If [$#-ne 2];then
    echo Error, please enter two Parameters. "
    exit 1
Else
    echo "You did a good job."
fi
[root@test script]# sh example.sh a
Error, please enter two parameters.
[Root@test script]# sh example.sh a B
You did a good job.
[Root@test script]# sh example.sh a b c
Error, please enter two parameters.
$_: Represents the last parameter of the previous command
$$: The pid

[Root@lamp script]# cat dollar.sh
#!/bin/bash
echo "$$" >/tmp/dollar.pid
while True
do
 &N bsp;  1
Done
[root@lamp script]# sh dollar.sh
################################################< br> [Root@lamp ~]# cat/tmp/dollar.pid
1483
[root@lamp ~]# ps-ef |grep 1483
Root    &nbs P 1483  1453  0 14:58 pts/1    00:00:00 sh dollar.sh
root      1532   1483  0 14:58 pts/1    00:00:00 sleep 1
root      1534  1496  0 14:58 pts/0    00:00:00 grep 1483
[root@lamp ~]# ps-ef |grep dollar
root  & nbsp;   1483  1453  0 14:58 pts/1    00:00:00 sh dollar.sh
root       1555  1496  0 14:58 pts/0    00:00:00 grep dollar

$!: The PID that represents the last-executed background command
$?: A flag that represents the success of the previous command, or $ if the execution succeeds? is 0, otherwise not 0.

[Byrd@lamp script]$ pwd
/byrd/script
[Byrd@lamp script]$ echo $?
0 #运行成功
[Byrd@lamp script]$ Ls/root
Ls:cannot Open Directory/root:permission denied
[Byrd@lamp script]$ echo $?
2 #权限拒绝
[Byrd@lamp script]$ hahaha
-bash:hahaha:command not found
[Byrd@lamp script]$ echo $?

127 #未找到该命令

###########################################
[byrd@lamp ~]$ cat/byrd/script/question_mark.sh
#!/bin/bash
#Example
Ls-al/root >/dev/null 2>&1
If [$-eq 0];then
    echo ' User is ro OT
Else
    echo "The user is not root"
fi
[root@lamp script]# sh question_mark.sh
Us ER is root
[root@lamp script]# su-byrd
[byrd@lamp ~]$ sh/byrd/script/question_mark.sh
The user is not root
not completed, pending finishing!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.