It might be worth mentioning that whilst push and pop sound lovely they are a really bad idea to use. You should always use relative or absolute paths and not change directories.
Well you change directories with cd, that's what cd means, change directory. I haven't run into any problems with pushd/popd at all in my experience. I use them in my alias's and my bash scripts and they've done perfectly fine so far. When you use pushd you are always using a relative or an absolute path.
Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
What happens if you call a function that changes the working directory to something like '/'.
Your code thinks that you are in /home/me/tempstuff
You then run 'rm -rf .'
You just deleted your entire file system.
What happens if another process deletes your working directory?
What directory are you in now?
By always using relative or absolute paths you know you are operating on the correct directory.
Yes, lots of people use cd/pushd/popd and yes, they work as expected.
The problem is that they are risky and best practice says to stay away from them.
Personally I dislike Bash to the extent that I've written a tool to replace it. If you are interested in using a modern programming language to write scripts have a look at:
It might be worth mentioning that whilst push and pop sound lovely they are a really bad idea to use. You should always use relative or absolute paths and not change directories.
Well you change directories with cd, that's what cd means, change directory. I haven't run into any problems with pushd/popd at all in my experience. I use them in my alias's and my bash scripts and they've done perfectly fine so far. When you use pushd you are always using a relative or an absolute path.
Whatβs the reasoning behind that? What pitfalls does this help avoid?
So there are several problems:
What happens if you call a function that changes the working directory to something like '/'.
Your code thinks that you are in /home/me/tempstuff
You then run 'rm -rf .'
You just deleted your entire file system.
What happens if another process deletes your working directory?
What directory are you in now?
By always using relative or absolute paths you know you are operating on the correct directory.
Yes, lots of people use cd/pushd/popd and yes, they work as expected.
The problem is that they are risky and best practice says to stay away from them.
Personally I dislike Bash to the extent that I've written a tool to replace it. If you are interested in using a modern programming language to write scripts have a look at:
pub.dev/packages/dshell