Linux中变量#,#,@,0,0,1,2,2,*,$$,$?的含义【转】

时间:2023-03-08 19:18:07
Linux中变量#,#,@,0,0,1,2,2,*,$$,$?的含义【转】

转自:http://www.cnblogs.com/kaituorensheng/p/4002697.html

1
2
3
4
5
6
7
8
$# 是传给脚本的参数个数
$0 是脚本本身的名字
$1 是传递给该shell脚本的第一个参数
$2 是传递给该shell脚本的第二个参数
$@ 是传给脚本的所有参数的列表
$* 是以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过9
$$ 是脚本运行的当前进程ID号
$? 是显示最后命令的退出状态,0表示没有错误,其他表示有错误

区别:@@*

  • 相同点:都是引用所有参数
  • 不同点:只有在双引号中体现出来。假设在脚本运行时写了三个参数(分别存储在112 3)则"3)则"*" 等价于 “112 3"(传递了一个参数);而“3"(传递了一个参数);而“@" 等价于 "1""1""2" "$3"(传递了三个参数)

例子一

Linux中变量#,#,@,0,0,1,2,2,*,$$,$?的含义【转】
##dels.sh
echo "number:$#"
echo "scname:$0"
echo "first :$1"
echo "second:$2"
echo "argume:$@"
echo "show parm list:$*"
echo "show process id:$$"
echo "show precomm stat: $?"
Linux中变量#,#,@,0,0,1,2,2,*,$$,$?的含义【转】

执行结果

1
2
3
4
5
6
7
8
9
[@jihite]$ sh del.sh 1 2 3
number:3
scname:del.sh
first: 1
second:2
argume:1 2 3
show parm list:1 2 3
show process id:21057
show precomm stat: 0

例子二

Linux中变量#,#,@,0,0,1,2,2,*,$$,$?的含义【转】
#!/bin/sh
num=$#
name=$0
echo "number:$num"
echo "scname:$name"
echo $0
echo $1
echo $2 for ((i=0; i<$num; i++))
do
echo "$i"
done echo "argume:$@"
for key in $@
do
echo $key
done
echo "-----------------"
for key in "$@"
do
echo $key
done
echo "-----------------------------"
for key2 in $*
do
echo $key2
done
echo "-----------------"
for key2 in "$*"
do
echo $key2
done echo "show process id:$$"
cho
echo "show precomm stat: $?"
Linux中变量#,#,@,0,0,1,2,2,*,$$,$?的含义【转】

执行结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[@jihite]$ sh del.sh a b                                                      
number:2
scname:del.sh
del.sh
a
b
0
1
argume:a b
a
b
-----------------
a
b
-----------------------------
a
b
-----------------
a b
show process id:23582
del.sh: line 37: cho: command not found
show precomm stat: 127

问题:

echo #0 #1 能不能用 $i 表示呢?

随机推荐

  1. Freemarker中通过request获得contextPath

    <!-- config Freemarker View Resolver--> <bean id="viewResolver" class="org.s ...

  2. HTTP深入浅出 http请求

    HTTP(HyperText Transfer Protocol)是一套计算机通过网络进行通信的规则.计算机专家设计出HTTP,使HTTP客户(如Web浏览器)能够从HTTP服务器(Web服务器)请求 ...

  3. linux时钟系统概述

    1. 了解下linux系统中一些时间概念,在kernel/time/timekeeping.c中定义了多个时间.RTC时间:在PC中,RTC时间又叫CMOS时间,通常由一个专门的计时硬件来实现,软件可 ...

  4. 设计视图不能用于 x64 和 ARM 目标平台

    设计视图不能用于 x64 和 ARM 目标平台

  5. python 内存监控模块之memory_profiler

    0. memory_profiler是干嘛的 This is a python module for monitoring memory consumption of a process as wel ...

  6. python待解决问题笔记

    2006, 'MySQL server has gone away' 描述:mysql服务端断开idle过期连接,而客户没有检测重连所以报错. 解决: def is_connection_usable ...

  7. javascript设计模式学习之九——命令模式

    一.命令模式使用场景及定义 命令模式常见的使用场景是:有时候需要向某些对象发送请求,但是并不知道请求的接受者是谁,也不知道请求的具体操作是什么.此时希望用一种松耦合的方式来设计程序,使得请求的发送者和 ...

  8. 使用dbms_system追踪其它session

    dbms_system是内部包,建议在官方指导下使用该包. SQL> desc dbms_system PROCEDURE ADD_PARAMETER_VALUE Argument Name T ...

  9. Leetcode: Combination Sum IV && Summary: The Key to Solve DP

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  10. html 标签内部元素上下居中

    <div style="width: 200px; height: 200px; border: 1px solid red; line-height: 200px;"&gt ...