What directory is '~' when I type 'cd ~'?

前端 未结 11 1758
伪装坚强ぢ
伪装坚强ぢ 2021-01-05 20:32

I\'m only new to using SSH, but when I log in I end up in the directory ~, which in general is the same directory when FTPing in. I can still go to /, but I don\'t know what

11条回答
  •  花落未央
    2021-01-05 21:35

    Different shells may or may not handle this differently, but Johnathan got the closest without coming out and saying it. The shell expands "~" to whatever's stored in the $HOME environment variable. The shell expands ~username to whatever's listed in the shell field of /etc/passwd for the given username. If you don't override it, the shell (or ssh, depending on the implementation) will set $HOME to be the home field from /etc/passwd, so they're both the same (assuming you're "username") until you change one.

    As to why you see a ~ in ssh...

    The prompt says "~" is your current directory most likely because you're using Bash as your shell, and the value of $PS1 (the prompt string you see - it's set in /etc/profile or /etc/profile.d/*, more than likely) contains a \w or a \W somewhere. The \w string in the prompt shows the current directory, and collapses to a "~" when you're in the directory specified by $HOME. Here's a little demo starting in my homedir - note how the "\w" gets replaced with either the current directory or with a ~, based on what the value of HOME is set to. Also note that the trailing slash doesn't work - HOME can't end with a slash for this to work. :)

    danny@machine ~ > export PS1='\w > '     # change my prompt (effective on next line)
    ~ > cd /tmp                              # move to /tmp to demonstrate
    /tmp > export HOME=/tmp/                 # set HOME to include trailing /
    /tmp > export HOME=/tmp                  # try again without trailing /
    ~ >                                      # notice that this works
    ~ > cd /home/danny                       # back to homedir
    /home/danny > export HOME=/home/danny    # see how it's /home/danny, not ~
    ~ > export PS1='danny@machine \w > '     # after resetting $HOME, it should work ok
    danny@machine ~ >                        # hooray!
    

提交回复
热议问题