64

I would like to display current path in sh prompt (not bash shell), which currently just shows "#", I tried with introducing this

env PS1="$(whoami)@$(hostname):$(pwd)"

and

set PS1="$(whoami)@$(hostname):$(pwd)"

in /etc/profile.

But as obvious this does not refresh when the the directory is changed or user changes. Please suggest a way to make this dynamic.

mpy
  • 28,816
Bleamer
  • 779

9 Answers9

98

Command substitutions in double quotes " get expanded immediately. That is not what you want for your prompt. Single quotes ' will preserve the substitutions in $PS1 which then get only expanded when displaying the prompt. Hence this should work:

PS1='$(whoami)@$(hostname):$(pwd)'

If you want the usual dollar sign and a space at the end of your prompt, simply add $ at the end (no escaping necessary): PS1='$(whoami)@$(hostname):$(pwd)$ '

mpy
  • 28,816
18
sh-4.2$ export PS1="\u@\h:\w>"
jenny@serenity:~>cd /usr/local
jenny@serenity:/usr/local>
Jenny D
  • 580
12

This command works for me.

export PS1="\u@\h: \W:$"

Where
\u = username
\h = hostname
\W Name of present folder (not full path)

8

One might consider to pimp the prompt by adding some colors. For instance:

export PS1='\[\e[0;36m\]\u\[\e[0m\]@\[\e[0;33m\]\h\[\e[0m\]:\[\e[0;35m\]\w\[\e[0m\]\$ '
Arvid
  • 181
  • 1
  • 4
2

Try this colorful MULTILINE prompt Add this line

export PS1="[\e[1;33m\u\e[m@\e[1;36m\h\e[m] [\$(date +%k:%M:%S)]\n\e[0;32m[\w]\e[m \n\$ "

Prompt will be:

[yourusername@hostname] [17:34:13]
~   <----- this will be your working directory
>
Toto
  • 19,304
Patis
  • 21
2

One answer was to use single quotes instead of double quotes, however, that's not quite the full correct answer. What you really want to do is defer evaluation of the code inside your prompt until the prompt is used.

set PS1="$(pwd)" 

sets the prompt to the working directory as of the set command.

set PS1="\$(pwd)" 

does NOT expand $(pwd). Instead, PS1 is set to the literal value of $(pwd).

Test / Understand this by running:

echo $PS1

. If you see the string : $pwd, your prompt works. If you see the literal path, the prompt is broken because it has been statically set

MaasSql
  • 131
1

Use the below command to set is like in the cpanel.

export PS1='$(whoami)@${HOSTNAME%%.*} [$(pwd)]# '

Milan
  • 31
0
  1. vim ~/.bashrc
  2. add following lines:
# notice the tailing space
export PS1='$(whoami)@$(hostname):$(pwd)# '
  1. open a new termal and you will find :
root@6e5efa720515:/opt/myapp#
Siwei
  • 677
0

You can display the current branch and folder path in the terminal in Ubuntu using the following methods, assuming you are working with a Git repository:

  1. Displaying Git Branch and Folder Path:

    To display the current Git branch and folder path, you can use the git command along with some shell commands. Here's a common approach to achieve this:

    Open your terminal and navigate to a directory within a Git repository. Then, add the following lines to your shell configuration file (e.g., ~/.bashrc, ~/.zshrc, ~/.bash_profile, or ~/.config/fish/config.fish, depending on your shell):

    For Bash or Zsh:

    parse_git_branch() {
    git branch 2>/dev/null | sed -n '/\* /s///p'
    }
PS1='${debian_chroot:+($debian_chroot)}\[\e[31m\][\[\e[m\]\[\e[38;5;172m\]\u\[\e[m\]@\[\e[38;5;153m\]\h\[\e[m\] \[\e[38;5;214m\]$(parse_git_branch)\[\e[m\] \[\e[38;5;214m\]\W\[\e[m\]\[\e[31m\]]\[\e[m\]\\$ '

When you open new terminal it will look like this:

[root@chief main current-folder]$