I am trying to set the PS1 prompt dynamically on the my remote machine. The idea is that when I will do ssh to the remote machine, I will also send the value which will set as a prompt for that remote machine. I already have an expect script which do automatic ssh login to the remote machine. Now I am trying to modifying the script in such a way that it will do automatic ssh along with setting the PS1 prompt with the value passed over ssh. Below is my script

#!/usr/bin/expect -f
set password Mayy@029Mayy@029
# now connect using ssh
spawn ssh -t [email protected]  "'export PS1='bvdev@429[\w]#'; exec bash -l"
expect "*?assword:*"
send -- "$password\r"
send -- "\r"

On executing the above script I am getting error. Inavalid command name w while executing \w.

I am not sure if I am approaching it in correct way? Is my script correct for this task. Please help me

  • 1
    I don't know the quoting rules of expect from the top of my head, by my guess is that you will have to escape the \w in the string and remove the '. Commented Dec 10, 2023 at 18:48
  • 2
    Other than that, other people solve that problem differently, simply by having the shell running on the remote check whether the user is logged in locally or remotely, and adjust the prompt as desired. This can be really easy in the ~/.bashrc on the target machine! Commented Dec 10, 2023 at 18:52
  • 3
    You have an odd number of single quotes. Sometimes that is OK, but I don't think so here.
    – shellter
    Commented Dec 10, 2023 at 19:03
  • @MarcusMüller I would like to do the way you are suggesting, setting the prompt from .bashrc file. But, we have different VM machines and .bashrc file is common for every VM. So I am not sure how can I set the dynamic PS1 prompt in .bashrc file such that for each VM, PS1 prompt should be set individually from the .bashrc file. The one idea which came in my mind is that the passing PS1 prompt over ssh for each VM. Please let me know if there is some other way possible. Thank you for giving your input
    – sachu
    Commented Dec 11, 2023 at 3:32
  • 1
    @MarcusMüller, yes, hostname will give the vm number and I am able to solve it. In my .bashrc file I have done export PS1="hostname | sed -r 's/.{5}/&@/g'[\w]#" and after this I am able to achieve what I wanted. Thank you for guiding
    – sachu
    Commented Dec 11, 2023 at 6:36

1 Answer 1


Because expect is an extension of Tcl, we have to be aware of the rules of Tcl syntax

spawn ssh -t [email protected]  "'export PS1='bvdev@429[\w]#'; exec bash -l"

Within double quotes, Tcl uses square brackets as the command substitution mechanism. Additionally, backslashes are acted upon: \w is not a special character (like \t is).

You'll want to use Tcl's non-interpolating quoting mechanism (like the shell's single quotes), which is curly brackets

spawn ssh -t [email protected]  {export PS1='bvdev@429[\w]#'; exec bash -l}

However, bash -l will process the user's dotfiles, so the exported PS1 you have set will most likely be ignored.

spawn ssh -t [email protected]  {
    export PS1='bvdev@429[\w]#'
    exec bash --noprofile --norc -i

However (again), I would not do that. I'd send the desired PS1 after logging in

spawn ssh -t [email protected] 
expect "*?assword:*"
send -- "$password\r"
send -- "\r"
send -- {PS1='bvdev@429[\w]#'}
send -- "\r"

You must log in to answer this question.

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