---
title: "在Linux上使用pushd的一些尝试"
author: "notfatboy"
date: "2011-08-11T04:54:17Z"
tags: []
categories: ["操作系统"]
slug: "linux-pushd"
draft: false
lastmod: "2011-08-11T05:33:13Z"
id: 1145251
url: "https://notfatboy.iteye.com/blog/1145251"
---

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目录。