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

PROBLEM DESCRIPTION or QUESTION


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

RESOLUTION


If you'd like to accomplish any of the following, Xshell must know the current working directory:

  • Moving to the working directory when cloning a session in Xshell

  • Move to the working directory when calling Xftp from Xshell

  • Have files and directories in the current working directory appear in File Manager

Therefore, 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 or PS1 environment variables are 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: https://tldp.org/HOWTO/Xterm-Title-4.html

  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. If you do, keep in mind that the path information should be located at the end of the title message. Xshell will parse the path from the first slash(/) or tilde(~) to the end of the title 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: