学习SHELL编程时遇到一个问题,是BUG还是我弄错了?

RedWolf1999 2011-06-29 03:31:48
我本来想试一下$@和$*这两个变量的区别的,于是使用vi编写一个简单的脚本如下:

root@ubuntu:/home/rebi/c# vi showarg
#!/bin/bash

eco "all argument list(@): $@"
echo "all argument list(*): $*"


#end

接着执行,不传递任何参数进去:
root@ubuntu:/home/rebi/c# . showarg
all argument list(@):
all argument list(*):
如上结果没有问题,参数为空。

但接下来,我使用set定义了一个叫str的串后,再不传递任何参数,执行,却发现$@和$*都变成了str:
root@ubuntu:/home/rebi/c# set str="hello"
root@ubuntu:/home/rebi/c# . showarg
all argument list(@): str=hello
all argument list(*): str=hello

于是果断unset str,再执行,发现问题依旧。。。
root@ubuntu:/home/rebi/c# unset str
root@ubuntu:/home/rebi/c# . showarg
all argument list(@): str=hello
all argument list(*): str=hello

如果带参数的话,就不会出现上面的str=hello。实在是搞不懂这是什么原因,在SuSE和Ubuntu下都试过了。
...全文
114 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
RedWolf1999 2011-07-02
  • 打赏
  • 举报
回复
哦了。感谢justkk大侠。
justkk 2011-06-30
  • 打赏
  • 举报
回复
set 就是给位置参数赋值的,第一个位置参数就是$1,第二个位置参数就是$2 ..

想清除的话,执行set --
RedWolf1999 2011-06-29
  • 打赏
  • 举报
回复
下面是我做的一个测试,好像每次第一个set定义的变量就会被默认保存成$1了。
login as: test1
Using keyboard-interactive authentication.
Password:
Last login: Wed Jun 29 12:35:14 2011 from 192.168.237.1
Welcome to rebi's SuSE, This Linux is only for me to study......
bogon:~ # echo $1

bogon:~ # echo $0
-bash
bogon:~ # echo $9

bogon:~ # set str1="hello"
bogon:~ # echo $1
str1=hello
bogon:~ # unset str1
bogon:~ # echo $1
str1=hello
bogon:~ #
RedWolf1999 2011-06-29
  • 打赏
  • 举报
回复
那这个str=hello到底是啥顽疾啊,怎么也删不掉了?
justkk 2011-06-29
  • 打赏
  • 举报
回复
set str="hello"
这样是给位置参数赋值,只有一个参数$1,值就是"str=hello”这一串

unset str 是没用的,没有str这个变量

19,612

社区成员

发帖
与我相关
我的任务
社区描述
系统使用、管理、维护问题。可以是Ubuntu, Fedora, Unix等等
社区管理员
  • 系统维护与使用区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧