How do I go back to a previous directory in bash?
File & Directory Commands
- To navigate into the root directory, use “cd /”
- To navigate to your home directory, use “cd” or “cd ~”
- To navigate up one directory level, use “cd ..”
- To navigate to the previous directory (or back), use “cd -“
Which command should I use to display the exit code of the previous command?
To check the exit code we can simply print the $? special variable in bash. This variable will print the exit code of the last run command.
How to use the cd command in bash scripts?
A Common Problem Let’s begin with a script that begins by navigating to the /var/log directory. We’ll call it varlog.sh: The idea here is that we navigate to /var/log, display the current folder to verify that it has changed, and finally display the process ID of the shell it’s running in.
What happens when you use the cd command?
If any of them match the target you provided in the cd command, you’re transferred to that directory. Now, regardless of where you are in the filesystem, when you use the cd command, the shell checks whether the target directory is located in the base directory. If it is, you’re moved to that target directory.
How to get back to previous directory in Bash?
Also usually available in OLDPWD in the environment, according to SUSv4, cd – is equvalent to My favorite trick to complement this is an alias, specifically ‘-‘ as an alias. Bash and Zsh, not sure about other shells: Now all you have to do is type ‘-‘ and you’re back at the previous directory. Very quick and handy!
How do I push CD back to previous Directory?
Use ‘pushd’ with a directory argument. It will push the current dir and the argument to a ‘stack’: Now you can go back and forth between those 2 using ‘cd -‘. Was wrong here. It does that but because ‘pushd’ changes dir to the argument specified.