开发者

how to access the directory stack from within a script?

开发者 https://www.devze.com 2023-04-06 04:08 出处:网络
I use the directory stack (listed with dirs, manipulated with pushd/popd) a lot in bash. I notice that when I run a script it has (its own shell probably, with) its own d.s.

I use the directory stack (listed with dirs, manipulated with pushd/popd) a lot in bash. I notice that when I run a script it has (its own shell probably, with) its own d.s.

Is there any way to access the d.s. in the shell that launched the script?

for instance if I want to do the same operation in all dire开发者_运维技巧ctories on the stack:

while [ $num -lt 0 ]
do
  num=`expr $num - 1`
  #TODO add operation here
  pushd +1
done

running this script just executes the same operation $num times in the current dir, because the scripts stack is empty.


You can use source to run a script in the context of your current bash process, but be aware that anything it does will affect your process - setting variables, changing dir, etc. It's equivalent to just typing the lines of the script directly.

0

精彩评论

暂无评论...
验证码 换一张
取 消