How to change shell prompt in Unix? - shell

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"

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.

Mac export PS1 doesn't handle escaped commands

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

Modifying bash prompt

When I open my terminal in Mac OS X, the command prompt current reads:
James-MacBook:project1 sam$
project1 is the name of the current directory.
What I want is to display the full path instead of James-MacBook.
How do I achieve this?
Your current prompt appears to show the hostname and the basename of your current directory. That means that the bash prompt, PS1, is likely set to:
PS1='\h:\W\$ '
To get the full directory name, use \w in place of \W:
PS1='\h:\w\$ '
You can set this at the command prompt. To make it permanent, this command can go into ~/.bashrc or ~/.bash_profile or other depending on how your system is configured.
You can read more about the options for command prompts, for which there are many options, in the PROMPTING section of man bash. Regarding the \w and \W options mentioned above, man bash explains how they are used:
\wthe current working directory, with $HOME abbreviated with a tilde (uses the value of the PROMPT_DIRTRIM
variable)
\W the basename of the current working directory, with $HOME abbreviated with a tilde
Easy. This is not an OS X thing, but a bash thing. Try this:
export PS1='$(pwd): '
Then if you want to make it permanent, just edit your .bash_profile:
nano ~/.bash_profile
And place that first command in there.

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. :)

Hiding the text before an input to only $-sign in Bash?

I have this in .bashrc;
PS1='$'
However, I see this still in terminal:
mas-macbook:some/path mas$
I want
$
PS1 should already have been exported long before you get to your .bashrc file, at least for a login shell. In that case, setting PS1 should simply overwrite the value (not its export status).
One thing to keep in mind is that bash itself does not run your .bashrc file for a login shell. The actual sequence of execution is:
/etc/profile, if there.
first of ~/.bash_profile, ~/.bash_login or ~/.profile.
I'm fairly certain that, if you want .bashrc to run for a login shell, it has to be sourced from one of those above.
For example, /etc/profile may call /etc/profile.local or all the scripts in the /etc/profile.d/ directory. Similarly, my .bash_profile calls the following, if they exist:
/etc/bash.bashrc
~/.bashrc
with the following snippet:
if [ -e /etc/bash.bashrc ] ; then
source /etc/bash.bashrc
fi
if [ -e "${HOME}/.bashrc" ] ; then
source "${HOME}/.bashrc"
fi
When I change PS1 and echo "hello" in my .bashrc, but comment out the sourcing of it in .bash_profile, the prompt doesn't get changed (nor the string printed) when I log in. When I uncomment the sourcing, I get both the string printed and the prompt changed when I log in.
To make sure that your .bashrc is called for your login shells, put that echo hello statement just after setting PS1, then log in to check.
If it is being called when you log in, you can execute "export -p" from your shell to get a list of all the exported variables - make sure PS1 has a "declare -x" in front of it. If not, just change your .bashrc to export it as well:
export PS1='$'
If it's already exported, then something is changing it after your set statement. In that case, you'll need to actually look at the login execution path to see what's getting called before it gives you control.
I think you need the export keyword:
export PS1='$'
You may have to escape the $ symbol. This works in my system...
PS1="\$"
If that don't work, please run the command 'echo $PS1' and let us know the results. You may also have to send the .bashrc file - it possible that you have set the PS1 variable before including the global /etc/bashrc file. If that's the case, the global file will overwrite the variable. You will have to set the PS1 variable after including the /etc/bashrc file.

Resources