shell push和popd

1. shell push和popd

1.1. push

➜  ~ dirs -v  #查看栈的情况
0	~

➜  ~ pushd one        #one入栈
~/one ~ -1
➜  one pushd two      #two入栈
~/one/two ~/one ~ -1
➜  two pushd three    #three入栈
~/one/two/three ~/one/two ~/one ~ -1
➜  three pushd four   #four入栈
~/one/two/three/four ~/one/two/three ~/one/two ~/one ~ -1
➜  four dirs -v       #查看栈的情况
0	~/one/two/three/four
1	~/one/two/three
2	~/one/two
3	~/one
4	~
5	-1
Once you've built up a stack, you can use it as a collection of bookmarks or fast-travel waypoints. For instance, assume that during a session you're doing a lot of work within the ~/one/two/three/four/five directory structure of this example. You know you've been to one recently, but you can't remember where it's located in your pushd stack. You can view your stack with the +0 (that's a plus sign followed by a zero) argument, which tells pushd not to change to any directory in your stack, but also prompts pushd to echo your current stack:

$ pushd +0
~/one/two/three/four ~/one/two/three ~/one ~/one/two/three/four/five

1.2. popd

➜  four popd      
~/one/two/three ~/one/two ~/one ~ -1
➜  three dirs -v
0	~/one/two/three
1	~/one/two
2	~/one
3	~
4	-1
You can also move backward in your stack. For instance, to quickly get to ~/one/two/three given the example output, you can move back by one, keeping in mind that pushd starts with 0:

$ pushd -0
~/one/two/three ~/one ~/one/two/three/four/five ~/one/two/three/four

1.3. 应用场景

应用在频繁移动路径

for target in $extra_targets
do
    echo "Building extra target: $target ..."
    pushd $target
    echo -e "\\033[1;31m"
    echo "Error occured while building extra target $target. Stop!"
    echo -e "\\033[0m"
    popd
    echo
done

https://opensource.com/article/19/8/navigating-bash-shell-pushd-popd

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值