Is it possible to save the last working directory when you do an "exit" in bash. So, the next time you login, it will be at the directory you were at when you last logged out.
相关问题
- How to get the return code of a shell script in lu
- JQ: Select when attribute value exists in a bash a
- Invoking Mirth Connect CLI with Powershell script
- Emacs shell: save commit message
- bash print whole line after splitting line with if
相关文章
- 使用2台跳板机的情况下如何使用scp传文件
- In IntelliJ IDEA, how can I create a key binding t
- Check if directory exists on remote machine with s
- shell中反引号 `` 赋值变量问题
- How get the time in milliseconds in FreeBSD?
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- BASH: Basic if then and variable assignment
Look at the .bashrc and perhaps being able to modify it at logout time to contain the directory you are interested in.
You most likely want to save the current directory to a file when the shell exits. There are a number of ways to detect the shell exitting:
You could save the CWD at every prompt, or every time you run 'cd', but that's a bit overkill.
To restore your CWD when the next shell starts is a simple matter of running 'cd' on the contents of your save file if it exists.
You may want to think about what should happen if you have multiple shells running at the same time. Which one do you want to save the CWD of? Should all new shells that start use that CWD? Or only just the next shell that starts?
You also need to be careful with this entire concept. If, for example, you manage to get your code run when you don't expect, it can confuse any number of things.
For example, if you were to run a shell script that changed the current working directory, and then, during exit, managed to save its CWD, you may end up, in the next shell that starts up, in a directory you don't expect.
Another popular one, at least for me at $work, is when a script is run when root su's to your user to run a script, all automatically (i.e., programmatically). If you're doing this CWD trick for your own user, that's fine, but it can majorly screw up with system tools that expect that "su - youruser -c ls" returns the files in your home directory. (Ok, calling "ls" as your user is a dumb idea, but there are things where your user may have a better environment for running something than root does - e.g., on NFS mounts that have root squashed.)
So, the key here is to tell if you're in an interactive shell or not, and, if not, don't restore or save your CWD. IIRC, $- has an 'i' for interactive shells. Use it for setting the trap and for restoring the CWD.
Personally, I just set an alias:
and then go.sh simply checks its parameter for which directory I want to go to. e.g., "go myproj" will cd ~/svn/myproj, and "go bigdisk" will cd /mnt/bigdisk. And, for more complex cases, it can take more parameters, e.g., "go myproj lib" would just "cd ~/svn/myproj/myproj/lib" or whatever. No automatic saving, but none of the side effects, either.
Put this in your
~/.bashrc
(probably at the end):Then you have a choice of the following:
Either put this in
~/.bash_logout
(will not remember directory for interactive non-login shells):Or use a
trap
onEXIT
, put this in your~/.bashrc
(traps can be overwritten easily/accidently):Or use
PROMPT_COMMAND
, put this your~/.bashrc
(probably the most reliable way):Also make sure that your
~/.profile
or~/.bash_profile
sources your~/.bashrc
, otherwise the path won't be restored when login shells start.See http://mywiki.wooledge.org/DotFiles for information on how you should use your dotfiles.
Save the last working directory in ~./.bash_logout into a hidden file:
Read this file in ~/.bashrc with
You can write current directory (pwd) to some file every time when changing dir (cd). When starting terminal, bash startup script should look that file for "last dir". If something found - cd to it.