Mac export PS1 doesn't handle escaped commands - bash

I'm just trying to change the terminal prompt in macOS Sierra. Nothing fancy, I just need to know the current path.
In my ~/.bash_profile I'm adding
export PS1="[\w] > "
I know that \w should be replaced by current full path but none of the escape characters seems to work on my Mac. I just get the same string without evaluating, so my prompt looks like
[\w] >
I've tried many different PS1 strings without luck. I also tried using .bashrc instead bash_profile.
Any ideas?

I suspect you're running a shell that doesn't understand the \w escape in the prompt string. If echo $0 returns something other than -bash, you have to use whatever that shell understands:
Zsh prompt expansion
PS1 in the POSIX shell

Related

How to customize the shell prompt in the VS Code terminal on macOS

I'm trying to customize my integrated terminal shell prompt in vscode, and was successfully able to change the theme (so that I can see my current working directory and branch I'm on), however now I want to remove the first portion 'anhlucci#Anhs-MacBook-Pro'. How do I do that?
I use Ubuntu with bash, and I only add the following lines to the end of ~/.bashrc:
if [ "$TERM_PROGRAM" = "vscode" ]; then
PS1='\[\033[01;34m\]\w\[\033[00m\]\$ '
fi
I found that vscode sets TERM_PROGRAM environment variable, and then use it to modify PS1 only to vscode.
The command line prompt is not dictated by Visual Studio Code, but by bash. The prompt is dictated by the PS1 variable in bash. You can view it as follows:
echo "$PS1"
To give you an idea of how that works, this is how my prompt looks like:
[hongli#Leticia Projects]$
My $PS1 looks like this:
[\u#\h \W]\$
Things like \u and \h are formatters that are substituted with a specific value. \u is for the current username, \h is for the hostname.
I'm guessing your $PS1 contains something like \u#\h in the beginning. Remove that and reset the PS1 variable, for example like this:
PS1='[\W]\$ '
Finally, you need to persist this in your bash configuration file so that the next time you start your shell it will show that same prompt. The bash config file is typically ~/.bashrc or ~/.profile depending on the exact Linux distribution you use. Make sure you set $PS1 in there.

Editing ~/.bash_profile doesn't customize command-line prompt properly

Related Issue: How to change the terminal prompt to just current directory?
I have added export PS1="\w\$ " to my ~/.bash_profile, but the prompt for the command line just displays this:
\w$
It recognizes the backslash escape for the $ character, but not for the filepath. It does the same thing when I use a capital 'W'.
The problem was that my terminal is running zsh. I created ~/.zshrc and wrote the following code:
PROMPT="%/\$ "
I referred to this resource to find the appropriate characters for displaying the current file path: ZSH Prompt Expansion.

How to change shell prompt in Unix?

I want to disable the Unix shell prompt character ($, #, %) which usually we see in terminal. Is there any command or setting which can do this? I am using Solaris OS.
By shell prompt character I mean:
>$
>#
You need to adjust your PS1 environment variable in your .profile file.
I guess you could set it to "" to have it empty.
ex:
export PS1=""
EDIT: it can also be in your .bashrc file, or any other shell you are using.
You can get fancy and put the host name in there. But basically you change the PS1 environment variable:
export PS1=hello
You can add this command in your ~/.bashrc file. Or other startup file, if you use another shell.
I suggest first check the man pages for the shell (whatever is yours? echo $SHELL) under shell variables.
There are four types of prompt strings(PS) PS1, PS2, PS3, PS4, for your problem PS1 adjustment is sufficient.
To check the current settings: echo $PS1
To change: PS1="" for the current session, to make it permanent export it in your ~/.bashrc or ~/.profile.
To make it permanent for the user: export PS1="whatever special characters you want"
for more special characters and examples you can visit here "http://linuxconfig.org/bash-prompt-basics"

Cygwin Terminal and zsh strange characters used in username

Hi I've recently installed zsh using cygwin on my Windows machince but when I type zsh to start this I get the following:
GG#GG-PC ~
$ zsh
\[\e]0;\w\a\]\n\[\e[32m\]\u#\h \[\e[33m\]\w\[\e[0m\]\n\$
On my mac I am using iTerm2 and this is so much easier to setup on here. Also I am having trouble in setting up the aliases and this is becauses its not setup properly in terms of config file where I can set this up in a separate file.
Any ideas how I can resolve?
It looks like zsh is inheriting the value of PS1 from the previous shell. The PS1 environment variable sets the shell prompt, and zsh used a different format for prompt substitutions than other shells. Try entering the following command after you start zsh:
PS1=$'%{\e]0;%d\a%}\n%F{green}%n#%m %F{yellow}%d%f\n%# '
If that works, add that line to your ~/.zshrc file.
That's also probably a good place to put your aliases.
There might be an issue because you launch zsh from bash actually and not cygwin.
One thing you can do is to launch zsh as the starting shell of mintty (the window that wraps your shell)
Create a shortcut with this inside:
c:\<cygwin-folder>\bin\mintty.exe -i /Cygwin-Terminal.ico /usr/bin/zsh --login -
Yo need to update .zshrc with your required theme and then
source .zshrc

How to suppress (or customize) Mac Terminal shell prompt

Currently in my Terminal, every shell prompt looks like ComputerName: FooDir UserName$. The UserName part simply wastes too much space out of my precious 80 columns. Is there a way to suppress it?
The prompt is defined by the environment variable PS1 which you can define in .bash_profile.
To edit it, open or create the (hidden) file .bash_profile:
nano .bash_profile
and add a line that says
export PS1=""
Between the quotation marks, you can insert what you would like as your terminal prompt. You can also use variables there:
\d – date
\t – time
\h – hostname
\# – command number
\u – username
\W – current directory (e.g.: Desktop)
\w – current directory path (e.g.: /Users/Admin/Desktop)
The default prompt for common Linux distributions would be \w $, which evaluates to ~ $ in your home directory or e.g. /Users $ somewhere else. There are also website (like this one) that can help you with building your prompt.
If you want to remove the UserName part, your choice would be \h: \w$.
Once you made your changes, save the file with Control+o, Return, Control+x.
Here's an excellent article with a full list of Variables and Colors:
Customize your Shell Command Prompt
For a simple, minimalistic prompt, you can try this. Add the following line to your .bash_profile or simply test it first by running it in your terminal:
export PS1="\[\033[0m\]\w\$ "
It'll look something like this:
Here's my Prompt (source), also very simple:
export PS1="\[\033[1;97m\]\u: \[\033[1;94m\]\w \[\033[1;97m\]\$\[\033[0m\] "
2019 onwards, MacOS default shell is Z Shell. To customize command prompt, add a file named .zshrc in user home and put following line that sets a PS1 environment variable with desired prompt format:
export PS1="[%n]%~> "
Open new terminal
This is result of following format expansion:
%n User name
%~ Current directory
See full list of available expansions here.
Your answer can be found right here:http://www.hypexr.org/bash_tutorial.php#vi at about the middle of the page. :)

Resources