How does Xshell determine the current working directory when calling Xftp?


When I call Xftp from Xshell, it opens to the user's home directory, not the directory I'm currently working on in Xshell.


You need to configure the terminal(shell) to relay its title message to Xshell upon every directory change. To do this, you'll need the following:

  1. The terminal title message code:  the message between the \033(\e)]0; and \007(\a) codes
  2. A setting where a command is executed every time new prompt is displayed.
    The PROMPT_COMMAND environment variable is used in BASH

Basically, a terminal emulator cannot know in which directory a user is working. But there is a way in which the terminal can determine the working directory under a restricted condition.

Some servers can utilize terminal code to tell a terminal emulator to change the terminal's title. If the terminal title contains information about the current working directory, the terminal emulator can process it.

This method of processing the current working directory can be used in Xshell when calling Xftp, duplicating sessions, or when the Remote File Manager lists files and directories.

The following example demonstrates how to change the terminal's title for the most frequently used shell, BASH. If you use a different shell, please refer to the following article:

  1. Use BASH or a BASH-like shell. 

  2. Set the TERM type to xterm or something similar (e.g.  xterm-color). This may not be mandatory in some of the more recent Unix/Linux systems.

    $ export TERM=xterm
  3. Configure the PROMPT_COMMAND variable to manage the title bar. Try the following:

    $ export PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"

    As you can see, the current directory information, $PWD, is included in the PROMPT_COMMAND.
    This is only an example. You can create your own desired title bar message.

  4.  Check if Xshell's title bar or tab changes after you change working directories.

    You can now see that the current working directory changed to /usr/bin: