0

So I have a script to open new terminal windows with 2 tabs, but when I run it, current directory in these tabs is not displayed. When using default ubuntu's bashrc it works fine, but changing \w to \W results in current folder not showing until using cd, after that it works ok. What's the problem here?

#!/bin/bash
gnome-terminal --tab  --working-directory="/mnt/0000000000/Users/Admin/Desktop/folder1/" --tab  --working-directory="/mnt/0000000000/Users/Admin/Desktop/folder2/"
exit 0

PS1s are:

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u:\W\$ '

and

case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u: \W\a\]$PS1"
    ;;
*)
    ;;
esac

Changing /Desktop/folder1/ to /Desktop/folder1 solves things. For some reason \W is sensitive to that last slash, while \w is not, yet path like "~/" woks fine in both cases.

5
  • 1
    Hard to say without seeing the script. What do you mean by \w and \W? In what directory are you when running the script? Normally the PS1 variable controls what's shown before the cursor, but since a cd fixes your problem, I don't think that's an issue here.
    – pfnuesel
    Commented Jan 18, 2018 at 14:52
  • I'm in the home directory. There are 3 PS1 instances in ubuntu's bashrc. If I leave \w (to display full path) in them, it shows full path in new window right away, but if I change it to \W (to display only last part of path) directory name will be missing till I go somewhere.
    – tyrande
    Commented Jan 18, 2018 at 15:05
  • please update with your PS1. I am unable to duplicate this behavior with my PS1, which is set to [\u@\h]-[$(date +'%a %b %e %H:%M %p')]:[\w] bash-[\!]\$ . If I change it to [\u@\h]-[$(date +'%a %b %e %H:%M %p')]:[\W] bash-[\!]\$ it still works as expected. Commented Jan 18, 2018 at 15:12
  • Made it work. I used "/folder1/folder2/" as my path1 and 2, and removing last / fixed it. Can you explain why this caused a problem and why "~/" in the script works even with slash there?
    – tyrande
    Commented Jan 18, 2018 at 15:16
  • @tyrande, please, edit your question to add any new information or other changes. It's hard to follow what the question is, if half of it is in the comments. Also, post that PS1.
    – ilkkachu
    Commented Jan 18, 2018 at 15:33

1 Answer 1

1

For some reason \W is sensitive to that last slash, […]

That reason is documented in the user manual for your shell program:

\W the basename of the current working directory […]

The basename as far as your shell is concerned is the portion following the final slash. The basename of /Desktop/folder1 is thus folder1, whereas the basename of /Desktop/folder1/ is the zero-length string that follows that final slash.

This is not quite the POSIX definition of a basename, and not what the basename command will tell you; but it is an approximation commonly used in Unix and Linux programs.

You must log in to answer this question.

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