3

I had previous changed the default directory my terminal opens up in to a specific sub folder in Documents no matter when or how you open a new terminal. I want to reset it back to the default behavior and I'm don't remember how I set the default to begin with.

Are there any other places that could setting my terminal directory on start up?

I've already reset the terminal profile back to to the default, checked $HOME is still default, and checked my .zshrc and .zprofile but don't see anything there that would change the default directory on start up.

.zshrc

# Add RVM to PATH for scripting. Make sure this is the last PATH variable change.
export PATH="/usr/local/opt/node@14/bin:$PATH"
export PATH="/usr/local/opt/[email protected]/bin:$PATH"
export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
export PATH="$HOME/.local/bin:$PATH"

.zprofile

# Setting PATH for Python 2.7
# The original version is saved in .zprofile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH

alias python=python3

# Setting PATH for Python 3.10
# The original version is saved in .zprofile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.10/bin:${PATH}"
export PATH

setting image

Update:

I found that in .zshenv would grab from .bash_profile, and in my .bash_profile there was a CD command to would set my directory. So even if you're using zshell you might be some things running from you bash settings.

5
  • Can you try using an alternative terminal app, eg iTerm2, to see if you can rule out Terminal being the issue? If you still end up in the wrong place with iTerm2, then the issue is in the shell startup. Commented Jun 7 at 10:14
  • To trace the startup (temporarily) you can put zsh in XTRACE mode. Terminal | Settings | General | Shells open with Command /bin/zsh -x. It'll get very verbose every time you issue a command, but will enable you to scroll back to the beginning and see if there is any reference to changing Documents. Commented Jun 7 at 10:25
  • Some other startup files to check, in addition to the usual suspects: /etc/zprofile, /etc/zshrc, and /etc/zshrc_Apple_Terminal.
    – Gairfowl
    Commented Jun 9 at 8:58
  • 1
    Just one more idea: Perhaps Apple's silly "shell session restore" feature gets in your way. Try to put the line : ${shell_session_save_user_state_functions:=} into your .zhsrc, then close all open zsh, create one new zsh, close this as well, and then open a new zsh once more. If you are lucky, the problem is gone. Commented Jun 11 at 8:04
  • 1
    Alternatively, you could put into your .zshenv the line export SHELL_SESSIONS_DISABLE=1. Commented Jun 11 at 8:10

3 Answers 3

3

Terminal's concept of the Default Working Directory is the users home directory ($HOME) as setup in Settings | Users and Groups (defaulted to /Users/USER). This information comes from the help screen for that Terminal settings dialog in the question (press ?).

Now, usually when users are setup or modified $HOME is not visible to be modified. But there is an 'Advanced' option to edit the user profile; UID, group, shell, $HOME. This is accessed by Control-Clicking the user in Users and Groups.

See https://support.apple.com/en-us/102547 for some background, though this is mostly to rename an account. In that scenario you'd be renaming the $HOME (eg. sudo mv /Users/fred /Users/bob), then changing the renamed user profile to point at the renamed $HOME. I would imagine that changing the home directory as has been done on your machine would be classed as 'unsupported'.

I would suggest that your $HOME has been changed in the past to /Users/USER/Documents/subfolder

I have setup a test user on my machine and set $HOME to /Users/USER/Documents, logged on as that user, opened Terminal, and verified that /Users/USER/Documents is the home directory.

As an administrator it should be sufficient to change it back to /Users/USER, and logoff / logon. If the affected account is already an admin, it will be safer to create another admin account to do the change with. Changing the affected account with the affected account may cause further problems!

However, once changed, there may be some other folders which will require moving from Documents to ensure proper operation of other apps. My test wasn't comprehensive enough beyond checking the home directory. But the change has indeed created new sub-folders for Desktop, Library, Downloads, Documents, .Trash; and the various .zsh* configs. Unpicking this may be tricky for you.

Of course, a reliable backup beforehand is essential.

Update:

Info from the OP (which I'd missed in the question tbf..) shows that $HOME is set correctly, yet the problem persists. This invalidates my answer somewhat, yet is still one way of achieving the problem.

3
  • Is there a way to check if the $HOME has been changed? When i echo $HOME it still says /Users/username This is my personal machine and while checking the only admin account, the Home directory is still /Users/username Commented Jun 7 at 7:17
  • Ah, well if echo $HOME returns the correct path, then my answer isn't the answer! That would be how you'd check, yes. I think I missed that info in your question. So, you logon, open a terminal session, pwd shows you to be in a subdir of Documents, yet echo $HOME is correct? The plot thickens... Commented Jun 7 at 9:54
  • 1
    This still will help others with similar issues +1
    – bmike
    Commented Jun 7 at 10:38
1

Since your zsh is an interactive login shell, the last startup file being processed is be your ~/.zlogin. If you want to cd to a certain directory initially for every interactive zsh,this could be where to put your cd command.

I would however recommend against it: It means that if every zsh login shell would end up in this directory. My personal suggestion would be that you ensure that you don't do any cd in any of your startup files, but set the initial directory in your terminal preferences. This allows you to have different terminal profiles, each with its own starting directory.

@AndyGriffiths pointed out in his comment, that you don't want to set a specific starting directory, but just want to have "the default". Actually, my answer covers this too, but I should have written it explicitly:

If you do not put any cd command, you will simply see the default behaviour. Hence, do not do a cd in your startup files.

zsh by itself does not set a "default" directory. It simply starts up in the directory of the parent process. Hence, there is no such default, and it does not make sense to ask for "a default place". What happens when you start a zsh from your terminal, is that your terminal sets one working directory initially (this is what you can configure in the preferences), and then it fires up the shell, and then zsh - depending on how it is invoked -, processes a series of startup files. As long as none of them changes the working directory, you are in the one set by the Terminal. It is important that you set in your preferences the options "new windows open with Default Working Directory", and the same for "new tabs open ....".

If you are not happy with this default, you have two choices: Either configure your zsh as described above, or install iTerm2, which has more possibilities to configure your terminal.

7
  • I have actually already edited something before and now I want to revert that change, I just don't remember what i actually did. I checked ~/.zlogin and there is nothing there that would that would change the directory. Commented Jun 7 at 7:21
  • For reverting your changes, just refer to the backup of your file, which you (or your texteditor) hopefully made. Commented Jun 7 at 7:34
  • What do you mean by there is nothing? SInce .zlogin is your file, and you define what should go in, you can put a cd command into the file, and believe me: This would then change your directory. Or, alternatively, you go with my suggestion and handle everything in the Terminal preferences. Commented Jun 7 at 7:35
  • 1
    @user1934428 You assume the OP knows what to revert. The issue as plainly presented in the Q is that they do not know what to revert, so are looking for clues to track it down. And you keep presenting solutions to achieve exactly what the OP does not want to do. Commented Jun 7 at 10:01
  • 1
    Please reread the question... and I quote from the OP, "I want to reset it back to the default behavior and I'm don't remember how I set the default to begin with.". Commented Jun 8 at 14:56
1

I found that in .zshenv would grab from .bash_profile, and in my .bash_profile there was a CD command to would set my directory. So even if you're using zshell you might be some things running from you bash settings.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .