It is necessary that in the system for each user his directory in which he was located before logging off was saved. And to the next login, the user automatically got into this directory.
2 answers
You can use the supported bash program to execute an arbitrary specified command at a point in time before displaying the prompt string.
when you use the bash program as an interactive shell, after executing the next command you entered, the program displays the prompt string. it usually looks something like
пользователь@хост текущий_каталог $ just before outputting this information, bash executes the command specified in the PROMPT_COMMAND environment PROMPT_COMMAND .
you can “play” with this by implementing in this command the saving of the name of the current directory (you can get it with the built-in command pwd ) in some file. for example, in ~/.lastpwd .
and when bash is loaded online, i.e., when executing a custom ~/.bashrc script, execute a command that reads the contents of the file and executes the cd прочитанное_значение .
Both of these points can be implemented with such a pair of lines added to the end of the script ~/.bashrc :
cd $(cat ${HOME}/.lastpwd 2>/dev/null) PROMPT_COMMAND="pwd > ${HOME}/.lastpwd" side effects:
- if you run another instance of the bash program in interactive mode (for example, by running another x-terminal emulator ), then the current directory in it will be the one that is currently the current directory of the first instance of the program.
- if more than one bash program is running interactively, and they currently have different directories, and you execute commands in one instance, then in the other (then in the third, etc.), then into the file
~/.lastpwdwill get the name of one or the other (the third, etc.) directory. in general, "who last updated the file, order and sneakers."
- I suggest an improvement - to save not in .lastpwd, but in .lastpwd $$ (to take the console console). Now, when we start, we can track this and pick up a few consoles with the correct paths. - KoVadim
- @jfs, copied from the wrong line. thanks, corrected. - aleksandr barakin
- @KoVadim, this whole line, three codes must be written! in my opinion, pulls on a separate answer. By the way, it will be especially cool somewhere in a month or a year, when thousands of x-terminals will be loaded at the start of the session. - aleksandr barakin
- but it is easy to be treated. After all terminals start up, you need to delete these files. The truth here is to not bang new. For this, first all these files need to be renamed, then you can also start the terminals. - KoVadim
- @KoVadim, it is simply treated - well, I usually from the two ways “not to be ill at all” and “to be treated simply” would prefer the first one. - aleksandr barakin
On exit (when executing the built-in exit in the login shell or when the interactive login shell is completed), bash reads ~/.bash_logout (if available).
To remember the last open directory in bash before exiting, you can add the file to ~/.bash_logout :
pwd > ~/.last-dir When a user logs in ( by su - <username> / password, using su - <username> or sudo -u me -i or via ssh or bash -l ), bash reads the first available file from ~/.bash_profile , ~/.bash_login , ~/.profile in the given order .
To automatically go to the last saved directory, you can add to the used startup file:
cd $(< ~/.last-dir) These files are NOT read if you just open Gnome Terminal (the non-login shell will be running).
If you want the latest working directory from any instance of bash to be used, without waiting for login / logout, use the @alexander barakin answer , which manipulates the prompt.
cdshellcd, there are also internalpushdandpopd. - aleksandr barakin