Hot answers tagged

214

Non-printable sequences should be enclosed in \[ and \]. Looking at your PS1 it has a unenclosed sequence after \W. But, the second entry is redundant as well as it repeats the previous statement "1;34". \[\033[01;32m\]\u:\[\033[01;34m\] \W\033[01;34m \$\[\033[00m\] |_____________| |_| | ...


195

Those are ANSI escape sequences; that link is to a chart of color codes but there are other interesting things on that Wikipedia page as well. Not all of them work on (e.g.) a normal Linux console. This is incorrect: \033]00m\] # white 0 resets the terminal to its default (which is probably white). The actual code for white foreground is 37. Also, ...


89

It is mostly to do with the size of the window assumed by the terminal is not the same as your actual window size. If you are using bash, you can try this. $ shopt checkwinsize If you don't get checkwinsize on Then activate it with $ shopt -s checkwinsize Then just attempt running another command (like ls) or resizing the window once, the above ...


69

Yes, this happens because it is a "partial line". And by default zsh goes to the next line to avoid covering it with the prompt. When a partial line is preserved, by default you will see an inverse+bold character at the end of the partial line: a "%" for a normal user or a "#" for root. If set, the shell parameter PROMPT_EOL_MARK can be used to ...


47

Looks like at least some of the list is: txtblk='\e[0;30m' # Black - Regular txtred='\e[0;31m' # Red txtgrn='\e[0;32m' # Green txtylw='\e[0;33m' # Yellow txtblu='\e[0;34m' # Blue txtpur='\e[0;35m' # Purple txtcyn='\e[0;36m' # Cyan txtwht='\e[0;37m' # White bldblk='\e[1;30m' # Black - Bold bldred='\e[1;31m' # Red bldgrn='\e[1;32m' # Green bldylw='\e[1;33m' # ...


47

Here is what the bash documentation says: PS1 The value of this parameter is expanded (see PROMPTING below) and used as the primary prompt string. The default value is ``\s-\v\$ ''. PS2 The value of this parameter is expanded as with PS1 and used as the secondary prompt string. The default is ``> ''. PS3 The value ...


44

You can use escape sequences in prompt variables. Put this in your ~/.bashrc: PS1='\w\$ '


43

> is the default continuation prompt.That is what you will see if what you entered before had unbalanced quote marks. As an example, type a single quote on the command line followed by a few enter keys: $ ' > > > The continuation prompts will occur until you either (a) complete the command with a closing quote mark or (b) type Ctrl+D ...


32

I wrote a bash function that can show you all the colors, if this helps. function colorgrid( ) { iter=16 while [ $iter -lt 52 ] do second=$[$iter+36] third=$[$second+36] four=$[$third+36] five=$[$four+36] six=$[$five+36] seven=$[$six+36] if [ $seven -gt 250 ];then seven=$[$seven-251]; fi ...


29

When you simply assign a value to a variable, the $(...) expression is evaluated unless it is enclosed in single quotes (or backslash-escaped). To understand, try and compare these two: A=$(pwd) echo "$A" B='$(pwd)' echo "$B" The value of A immediately becomes the string /home/yourusername and obviously it's not remembered where this string comes from, it ...


29

That's the shell prompt, or more precisely, it's the shell's primary prompt (there are several). It's the shell's way of saying "go ahead, I'm ready for input now". The % prompt is common in csh-type shells while sh-shells (like bash and ksh93) ordinarily uses a $ as the prompt. The prompt usually changes to # for the root user since a sufficiently ...


27

To get a similar effect like in bash, that is including the ..., try: %(4~|.../%3~|%~) This checks, if the path is at least 4 elements long (%(4~|true|false)) and, if true, prints some dots with the last 3 elements (.../%3~), otherwise the full path is printed %~. I noticed that bash seems to shorten paths in the home directory differently, for example: ...


26

This is changed by changing the environment variable PS1. You can see the current value of PS1 by: root@monu dev# echo $PS1 You can see the value to be equal to \u@\h \w\$, where: \u : username \h : hostname \w : current working directory \$ : a # symbol for the root user, a $ symbol for other users If you want the change to be permanent, you have to ...


25

What's going on is that Bash is getting confused about the number of printing characters in your prompt. It sends cursor positioning sequences to the terminal to position the cursor properly for doing command history and such. It needs to have a good idea of where the cursor actually is after printing the prompt. Try setting your prompt to this: PS1='\[\e[...


23

Bash's prompt control features are rather static. If you want more control, you can include variables in your prompt; make sure you haven't turned off the promptvars option. PS1='${PWD#"${PWD%/*/*}/"} \$ ' Note the single quotes: the variable expansions must happen at the time the prompt is displayed, not at the time the PS1 variable is defined. If you ...


22

Bash has a precommand hook. Sort of. preexec () { clear } preexec_invoke_exec () { [ -n "$COMP_LINE" ] && return # do nothing if completing [ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return # don't cause a preexec for $PROMPT_COMMAND local this_command=`history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//g"`; # obtain ...


20

The solution is to get the shell to substitute the color variables when defining the prompt, but not the functions. To do this, use the double quotes as you had originally tried, but escape the commands so they aren't evaluated until the prompt is drawn. PS1="\u@\h:\w${YELLOW}\$(virtual_env)${GREEN}\$(git_branch)${RESET}$ " Notice the \ before the $() on ...


20

You can ask password by means of GUI prompt with the help of -A, --askpass options for sudo. From the sudo manpage: -A, --askpass Normally, if sudo requires a password, it will read it from the user's terminal. If the -A (askpass) option is specified, a (possibly graphical) helper program is executed to read the user's ...


19

Put \j in your prompt. From the bash manual: \j The number of jobs currently managed by the shell Just remember that prompts do go stale and jobs can finish at any time, so if you have left the terminal idle, you'll want to redisplay the prompt. At the cost of requiring an extra process just to print your prompt, you can make the \j only appear if ...


18

That's a specific feature of zsh (and now fish as well) to let you clearly see unterminated lines in a command's output. In traditional shells, if a command outputs some data after the last newline character, or, in other words, if it leaves the terminal cursor not at the start of the line, the next prompt by the shell ends up appended to that last ...


17

Most unix tools are designed to work well with text files. A text file consists of a sequence of lines. A line consists of a sequence of printable characters ending with a newline character. In particular, the last character of a non-empty text file is always a newline character. Evidently, example.txt contains only some text with no final newline, so it is ...


17

Look into your ~/.bashrc or ~/.profile, there may be a commented prompt setup that should do what you want, like this one on our infra: export PS1='\h:\w\$ ' Which looks like: coolservername:~# Or if you plan on logging as non-root, you can use: export PS1='\u@\h:\w\$ ' to add username before the hostname. You can have fun adding colours, multiline ...


16

sudo executes its argument using exec, not via a shell interpreter. Therefore, it is limited to actual binary programs and cannot use shell functions, aliases, or builtins (if is a builtin). Note that the -i and -s options can be used to execute the given commands in a login or non-login shell, respectively (or just the shell, interactively; note that you'...


16

That will happen if you have an unclosed quote in your command. That's something like: $ echo "test here > > ... You can exit that mode by closing the quote (write a " or ', or whatever your open quote is). It could also be a brace-delimited block, a partially-complete for-do or while-do loop, or certain other constructs. You can also press Ctrl-C to ...


16

First of all, you might simply want to change the \w with \W. That way, only the name of the current directory is printed and not its entire path: terdon@oregano:/home/mydirectory1/second_directory_with_a_too_long_name/my_actual_directory_with_another_long_name $ PS1="\u@\h:\W \$ " terdon@oregano:my_actual_directory_with_another_long_name $ That might ...


16

Historically the original /bin/sh Bourne shell would use $ as the normal prompt and # for the root user prompt (and csh would use %). This made it pretty easy to tell if you were running as superuser or not. # is also the comment character, so anyone blindly re-entering data wouldn't run any real commands. More modern shells (eg ksh, bash) continue this ...


15

To get the same output you note in your question, all that is needed is this: PS1='${PS2c##*[$((PS2c=0))-9]}- > ' PS2='$((PS2c=PS2c+1)) > ' You need not contort. Those two lines will do it all in any shell that pretends to anything close to POSIX compatibility. - > cat <<HD 1 > line 1 2 > line $((PS2c-1)) 3 > HD line 1 ...


15

Because the doc says so: https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#Bash-Variables PS3 The value of this variable is used as the prompt for the select command. If this variable is not set, the select command prompts with ‘#? ’


14

Make sure that the prompt_subst option is turned on. If necessary, add the following line to your ~/.zshrc: setopt prompt_subst This tells zsh to reevaluate the prompt string each time it's displaying a prompt. Then, make sure you are assigning PS1 (or some other variable that is used by the prompt theme) as desired: PS1='${PWD/#$HOME/~}' The single ...


14

I prefer the following method... cat example.txt ; echo This doesn't doesn't evaluate the contents of example.txt or occasionally add a newline. It just echos a newline once the cat is done, is easy to remember, and no one is thinking about whether they're using strong or weak quoting correctly. The only downside, really, is that you'll get an extra ...


Only top voted, non community-wiki answers of a minimum length are eligible