在Linux上使用pushd的一些尝试

Linux上使用pushd的时候,遇到了奇怪的问题,当我在/root/test folder下创建了tmp1,tmp2,tmp3 三个文件夹

drwx-----T 5 root root 4096 2011-08-11 05:09 ./
drwx------ 8 root root 4096 2011-08-10 05:21 ../
drwxr-xr-x 2 root root 4096 2011-08-11 05:09 tmp1/
drwxr-xr-x 2 root root 4096 2011-08-10 05:09 tmp2/
drwxr-xr-x 2 root root 4096 2011-08-10 05:09 tmp3/

 然后执行在test folder下执行pushd,如下:

root@computer:~/test# pushd ./tmp1
~/test/tmp1 ~/test ~/test
root@computer:~/test/tmp1# dirs -v
 0  ~/test/tmp1
 1  ~/test
 2  ~/test

 
此时我们是在tmp1目录下,执行cd ..回到上一级目录,再进行dirs查询:

root@computer:~/test/tmp1# cd ..
root@computer:~/test# dirs -v
 0  ~/test
 1  ~/test
 2  ~/test
root@computer:~/test#

 

诡异的一幕出现了,栈中原来的目录没有了,全部换成了~/test。至今不明白为什么会这样。

但是当我使用以下面的方式使用pushd的时候则没有此问题

root@computer:~/test/tmp1# dirs
~/test/tmp1
root@computer:~/test/tmp1# pushd .
~/test/tmp1 ~/test/tmp1
root@computer:~/test/tmp1# cd ..
root@computer:~/test# cd tmp2
root@computer:~/test/tmp2# pushd .
~/test/tmp2 ~/test/tmp2 ~/test/tmp1
root@computer:~/test/tmp2# cd ..
root@computer:~/test# dirs -v
 0  ~/test
 1  ~/test/tmp2
 2  ~/test/tmp1

 

 或者我们可以使用-n参数,如下:

root@computer:~/test# dirs
~/test
root@computer:~/test# pushd -n ./tmp1
~/test ./tmp1
root@computer:~/test# pushd -n ./tmp2
~/test ./tmp2 ./tmp1
root@computer:~/test# pushd -n ./tmp3
~/test ./tmp3 ./tmp2 ./tmp1
root@computer:~/test# dirs -v
 0  ~/test
 1  ./tmp3
 2  ./tmp2
 3  ./tmp1

 在这里,-n参数只将其后的参数即目录压入到栈中,但是并不切换目录,当前目录始终是test目录。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值