Macos Bash Prompt For Input With Default

< Bash

Bash has several prompts which can be customized to increase productivity, aesthetic appeal, and nerd cred.

Prompts

Bash and Z Shell. Bash, otherwise known as the Bourne Again Shell, started in 1989 to replace the older Bourne shell. It’s been the default shell in macOS starting with OS X Jaguar released in 2002. Macos Profile For Bash Mach-o But Built For Simulator Not Macos C Mackeeper Update For El Capitan Why Linux Is Better Than Windows Or Macos For Security Free Pages For Mac Yosemite Will Aws Amplify Work For Macos Radarr For Mac Mojave Disney Gift Card For El Capitan Theater. At the end of your 'Target:' line add the command line flags. For example: -disable-gpu-vsync; With that example flag, it should look like below (replacing '-disable-gpu-vsync' with any other command line flags you want to use): chrome.exe -disable-gpu-vsync; Launch Chrome like normal with the shortcut. Quit any running instance of. See full list on howtogeek.com. See full list on ryanstutorials.net.

Bash has four prompt strings that can be customized:

  • PS1 is the primary prompt which is displayed before each command, thus it is the one most people customize.
  • PS2 is the secondary prompt displayed when a command needs more input (e.g. a multi-line command).
  • PS3 is not very commonly used. It is the prompt displayed for Bash's select built-in which displays interactive menus. Unlike the other prompts, it does not expand Bash escape sequences. Usually you would customize it in the script where the select is used rather than in your .bashrc.
  • PS4 is also not commonly used. It is displayed when debugging bash scripts to indicate levels of indirection. The first character is repeated to indicate deeper levels.
Prompt

All of the prompts are customized by setting the corresponding variable to the desired string (usually in ~/.bashrc), for example

Techniques

While one can simply set their prompt to a plain string, there are a variety of techniques for making the prompt more dynamic and useful.

Bash escape sequences

When printing the prompt string, Bash looks for certain backslash-escaped characters and will expand them into special strings. For example, u is expanded into the current username and A is expanded to the current time. So a PS1 of 'A u $ ' would be printed like 17:35 username $ .

See the man page bash(1) § PROMPTING or the Bash reference manual for a complete list of escape sequences.

Macos Bash Prompt For Input With Default Download

Terminfo escape sequences

Aside from the escape characters recognized by Bash, most terminals recognize special escape sequences that affect the terminal itself rather than printing characters. For example they might change the color of subsequent printed characters, move the cursor to an arbitrary location, or clear the screen. These escape sequences can be somewhat illegible and can vary from terminal to terminal, so they are documented in the terminfo database. To see what capabilities your terminal supports, run

The capability names (the part before the =) can be looked up in terminfo(5) for a description of what they do. For example, setaf sets the foreground color of whatever text is printed after it. To get the escape code for a capability, you can use the tput command. For example

prints the escape sequence to set the foreground color to green.

Note: If tput commands are failing for you, ensure that you have set the correct TERM value for your shell. For example, if you have set xterm instead of xterm-256color, tput setaf will only work with color numbers 0-7.

To practically incorporate these capabilities into your prompt, you can use Bash's command substitution and string interpolation. For example

Note: Wrapping the tput output in [ ] is recommended by the Bash man page. This helps Bash ignore non-printable characters so that it correctly calculates the size of the prompt. The wrap will not work with command substitution, in which case the raw 1 2 must be used.

ANSI escape sequences

Unfortunately, valid ANSI escape sequences may be missing from your terminal's terminfo database. This is especially common with escape sequences for newer features such as 256 color support. In that case you cannot use tput, you must input the escape sequence manually.

See Wikipedia:ANSI escape code for examples of escape sequences. Every escape sequence starts with a literal escape character, which you can input using the Bash escape sequence e. So for example,e[48;5;209m sets the background to a peachy color (if you have 256 color support) and e[2;2H moves the cursor near the top-left corner of the screen.

In cases where Bash escape sequences are not supported (such as PS3) you can get a literal escape character using Bash's printf builtin:

Embedding commands

If you want to add the output of some command to your prompt, you might be tempted to use command substitution. For example, to add the amount of free memory to your prompt you might try:

But this doesn't work; the amount of memory shown is the same every time! This is because the command is run once, when PS1 is first set, and never again. The trick is to simply prevent the substitution either by escaping the $ or by defining it in single quotes—either way it will be substituted when the prompt is actually displayed:

To prevent long commands from making your PS1 huge, you can define functions:

Note: You can use terminfo/ANSI escape sequences inside substituted functions but not Bash escapes. In particular [ ] won't work for surrounding non-printable characters. Instead you can use the octal escapes 001 and 002 (e.g. using printf or echo -e).

PROMPT_COMMAND

If the PROMPT_COMMAND variable is set, it will be evaluated right before PS1 is displayed. This can be used to achieve quite powerful effects. For example it can reassign PS1 based on some condition, or perform some operation on your Bash history every time you run a command.

Warning: PROMPT_COMMAND generally should not be used to print characters directly to the prompt. Characters printed outside of PS1 are not counted by Bash, which will cause it to incorrectly place the cursor and clear characters. Either use PROMPT_COMMAND to set PS1 or look at embedding commands.
Tip: If the PROMPT_COMMAND becomes too complicated, bash-preexec (an implementation of Zsh's preexec and precmd hook functions for Bash) may make it more maintainable.

Escapes between command input and output

You can affect your input text in Bash by not resetting the text properties at the end of your PS1. For example, inserting tput blink at the end of your PS1 will make your typed commands blink. However this effect will also continue through the command's output since the text properties are not reset when you hit Enter.

In order to insert escape sequences after you type a command but before the output is displayed, you can trap Bash's DEBUG signal, which is sent right before each command is executed:

Customizing root prompts

To ensure that you know when you are running as root, you can customize your root prompt to make it clearly stand out (perhaps blinking red?). This is done by customizing the Bash prompt as usual but in root's home directory, /root. Start off by copying the skeleton files /etc/skel/.bash_profile and /etc/skel/.bashrc to /root, then edit /root/.bashrc as desired.

Examples

Colors

This article or section needs language, wiki syntax or style improvements. See Help:Style for reference.

Reason: Too much duplication with Color output in console WRT listing colors. Should be trimmed to zsh length. (Discuss in Talk:Bash/Prompt customization#)
Tip:infocmp shows the number of colors tput works with, for example colors#8.

To see the full range of colors your terminal supports, you can use a simple loop with tput (change setab to setaf for text foregrounds):

If that does not work (and you cannot fix it by setting the correct TERM value), you can test the different manual escape sequences:

To change the manual escapes from background to foreground, the standard color range is 30..37, the high intensity range is 90..97, and the 48 should be changed to 38 for 256 colors.

Common capabilities

The following terminfo capabilities are useful for prompt customization and are supported by many terminals. #1 and #2 are placeholders for numeric arguments.

CapabilityEscape sequenceDescription
Text attributes
blinkE[5mblinking text on
boldE[1mbold text on
dimE[2mdim text on
revE[7mreverse video on (switch text/background colors)
sitmE[3mitalic text on
ritmE[23mitalic text off
smsoE[7mhighlighted text on
rmsoE[27mhighlighted text off
smulE[4munderlined text on
rmulE[24munderlined text off
setab #1E[4#1mset background color #1 (0-7)
setaf #1E[3#1mset text color #1 (0-7)
sgr0E(BE[mreset text attributes
Cursor movement
scE7save cursor position
rcE8restore saved cursor position
clearE[HE[2Jclear screen and move cursor to top left
cuu #1E[#1Amove cursor up #1 rows
cud #1E[#1Bmove cursor down #1 rows
cuf #1E[#1Cmove cursor right #1 columns
cub #1E[#1Dmove cursor left #1 columns
homeE[Hmove cursor to top left
hpa #1E[#1Gmove cursor to column #1
vpa #1E[#1dmove cursor to row #1, first column
cup #1#2E[#1;#2Hmove cursor to row #1, column #2
Removing characters
dch #1E#1Premove #1 characters (like backspacing)
dl #1E#1Mremove #1 lines
ech #1E#1Xclear #1 characters (without moving cursor)
edE[Jclear to bottom of screen
elE[Kclear to end of line
el1E[1Kclear to beginning of line

Visualizing exit codes

Using the same trick from embedding commands you can delay the interpolation of special Bash variables like $?. So the following prompt shows the exit code of the previous command:

This can be made more interesting using conditionals and functions:

:) > true
:) > false
D: >

Positioning the cursor

It is possible to move the cursor around the screen inside of PS1 to make different parts of the prompt appear in different locations. However, to ensure that Bash positions the cursor and output in the right position, you must move the cursor back to the original position after you are done printing elsewhere. This can be done using the tput capabilities sc and rc to save and restore the cursor position. The general pattern for a prompt that moves the cursor is

where the entire block of repositioned prompt is wrapped in [ ] to prevent Bash from counting it as part of the regular prompt.

Right-justified text

The simplest way to print text on the right side of the screen is to use printf

This creates a right-justified variable-sized field %*s and sets its size to the current number of columns of the terminal $COLUMNS.

Arbitrary positioning

The cup capability moves the cursor to a specific position on the screen, for example tput cup 20 5 moves the cursor to line 20, column 5 (where 0 0 is the top left corner). cuu, cud, cuf, and cub (up, down, forward, back) move the cursor relative to its current position. For example tput cuf 10 moves the cursor 10 characters to the right. You can use the LINES and COLUMNS variables in the arguments to move the cursor relative to the bottom and right edges. For example, to move 10 lines and 5 columns away from the bottom right corner:

Customizing the terminal window title

The terminal window title can be customized in much the same way as the prompt: by printing escape sequences in the shell. Thus it is common for users to include window title customizations in their prompt. Although this is technically a feature of xterm, many modern terminals support it. The escape sequence to use is ESC]2;new titleBEL where ESC and BEL are the escape and bell characters. Using #Bash escape sequences, changing the title in your prompt looks like

Of course your window title string can include output from embedding commands or variables such as $PWD, so that the title changes with each command.

See also

  • Community examples and screenshots in the Forum thread: What's your PS1? (only accessible if logged in)
  • Gentoo's /etc/bash/bashrc. See also gentoo-bashrcAUR.
  • tput(1)

Macos Defaults Command

Retrieved from 'https://wiki.archlinux.org/index.php?title=Bash/Prompt_customization&oldid=668511'

Question – How to add [Y/n] confirmation in our own shell scripts? How to take input from the user as Yes/No/Y/N in bash?

Many times you have seen commands ask for confirmation [Y/n] or [Yes/No] input. This is a very useful part to know if a user wants to proceed with the remaining steps for not. You can also add the same function to your script. This article will help you with examples of (Bash Script – Prompt to Confirm (Y/N, YES/NO)) this type of inputs.

#1. Bash (Yes/No) Prompt

This example code will prompt for confirming once if you give wrong input, the program will exit with status 1. The below example code will accept only Y or n or yes or no (Not case-sensitive).

Input

Macos Bash Prompt For Input With Default Browser

2
4
6
8
10
12
14
16
18
20
do
[yY][eE][sS]|[yY])
break
[nN][oO]|[nN])
break
*)
;;
done