LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Appending to PROMPT_COMMAND (https://www.linuxquestions.org/questions/linux-newbie-8/appending-to-prompt_command-835322/)

ahtoot 09-29-2010 10:44 PM

Appending to PROMPT_COMMAND
 
I have the following as my $PROMPT_COMMAND in .bashrc:

Code:

PROMPT_COMMAND='
if [ $TERM = "screen" ]; then
 MYPWD="${PWD/#$HOME/~}"
 [ ${#MYPWD} -gt 20 ] && MYPWD=..${MYPWD:${#MYPWD}-18}
 echo -n -e "\033k$MYPWD\033\\"
fi
'

I am trying to append items to my PROMPT_COMMAND in another script/on command line. If I do the following:

Code:

export PROMPT_COMMAND="$PROMPT_COMMAND && ls"
bash has a syntax error and complains of unexpected token near &&.
But if I change the following in .bashrc

Code:

PROMPT_COMMAND='
if [ $TERM = "screen" ]; then
 MYPWD="${PWD/#$HOME/~}"
 [ ${#MYPWD} -gt 20 ] && MYPWD=..${MYPWD:${#MYPWD}-18}
 echo -n -e "\033k$MYPWD\033\\"
fi && ls
'

I get no error.

I've echoed the value of $PROMPT_COMMAND in both cases and their output is exactly the same.


Any tips? Thanks.

Meson 09-29-2010 11:08 PM

It probably has to do with the fact that you already have quotes inside the original prompt command.

grail 09-29-2010 11:33 PM

I am curious what you expect to happen?

If you did the following on the command line:
Code:

PROMPT_COMMAND="$PROMPT_COMMAND && ls"
And now perform an echo, all that has happened is '&& ls' gets appended to the string.

The questions then becomes, what is processing $PROMPT_COMMAND?

ahtoot 09-30-2010 01:08 AM

Quote:

Originally Posted by grail (Post 4113309)
I am curious what you expect to happen?

If you did the following on the command line:
Code:

PROMPT_COMMAND="$PROMPT_COMMAND && ls"
And now perform an echo, all that has happened is '&& ls' gets appended to the string.

The questions then becomes, what is processing $PROMPT_COMMAND?

bash is processing $PROMPT_COMMAND
http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x264.html


I've solved it, I had to put the last quote on the same line as the last fi

Code:

PROMPT_COMMAND='
if [ $TERM = "screen" ]; then
 MYPWD="${PWD/#$HOME/~}"
 [ ${#MYPWD} -gt 20 ] && MYPWD=..${MYPWD:${#MYPWD}-18}
 echo -n -e "\033k$MYPWD\033\\"
fi'


Now if someone can tell me why the previous version didn't work..

Code:

PROMPT_COMMAND='
if [ $TERM = "screen" ]; then
 MYPWD="${PWD/#$HOME/~}"
 [ ${#MYPWD} -gt 20 ] && MYPWD=..${MYPWD:${#MYPWD}-18}
 echo -n -e "\033k$MYPWD\033\\"
fi
'

Edit: Maybe because && can't start on a newline?

catkin 09-30-2010 01:12 AM

Because the fi command needs to be terminated -- by a ; or by a newline.


All times are GMT -5. The time now is 07:07 PM.