Tag Info

Hot answers tagged

41

You can use any printable character, bash doesn't mind. You'll probably want to configure your terminal to support Unicode (in the form of UTF-8). There are a lot of characters in Unicode, so here are a few tips to help you search through the Unicode charts: You can try to draw the character on Shapecatcher. It tries to recognize a Unicode character in ...


40

The UNIX standard provides example code for this using the locale utility: if printf "%s\n" "$response" | grep -Eq "$(locale yesexpr)" then affirmative processing goes here else non-affirmative processing goes here fi The value for 'yesexpr' in the POSIX locale (and on English locales on real systems) is "^[yY]". It is to be interpreted as an ...


35

Here are a couple of things you can do: Editors + Code A lot of editors have syntax highlighting support. vim and emacs have it on by default. You can also enable it under nano. You can also syntax highlight code on the terminal by using Pygments as a command-line tool. grep grep --color=auto highlights all matches. You can also use export ...


21

Tmux sets the TMUX environment variable in tmux sessions, and sets TERM to screen. This isn't a 100% reliable indicator (for example, you can't easily tell if you're running screen inside tmux or tmux inside screen), but it should be good enough in practice. if ! { [ "$TERM" = "screen" ] && [ -n "$TMUX" ]; } then PS1="@$HOSTNAME $PS1" fi If you ...


18

I also use: export TERM=xterm-color export GREP_OPTIONS='--color=auto' GREP_COLOR='1;32' export CLICOLOR=1 export LSCOLORS=ExFxCxDxBxegedabagacad And if you like colorizing your prompt, defined color vars can be useful: export COLOR_NC='\e[0m' # No Color export COLOR_WHITE='\e[1;37m' export COLOR_BLACK='\e[0;30m' export COLOR_BLUE='\e[0;34m' export ...


17

Find where your PS1 is set in your .bashrc and insert '\[\e[1m\]' at the beginning and \[\e[0m\] at the end. \[ and \] are necessary so the shell knows the mess inside takes up 0 space on the screen, which prevents some screwed up behavior when doing line-editing. You don't need to worry too much about it. \e[ is known as the CSI (control sequence ...


17

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


16

The following settings seem to work. The text on the second line disappears if the command line overflows the first line. The preexec function erases the second line before running the command; if you want to keep it, change to preexec () { echo; }. terminfo_down_sc=$terminfo[cud1]$terminfo[cuu1]$terminfo[sc]$terminfo[cud1] PS1_2='[some status]' ...


14

The text displayed before the login prompt is stored in /etc/issue (there's a related file, /etc/motd, that's displayed after the user logs in, before their shell is started). It's just a normal text file, but it accepts a bunch of escape sequences: \b -- Baudrate of the current line. \d -- Current date. \s -- System name, the name of the operating system. ...


11

You've already found zle-keymap-select which is executed whenever the mode changes. You could use it to set some other visual indicator than the prompt, depending on what your terminal supports it (and your taste in mode indicator display, of course). There is a standard terminfo capability to change the shape of the cursor. However some terminals display ...


11

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


10

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


10

You should read about .bash_profile and .bashrc. The bracketed information is just a preference in those files which typically live in your home directory and/or /etc/profile (look for the line that starts PS1=). I'm assuming that when you're chrooted, that user does not have access to its home folder to load these files, so your prompt is, essentially, ...


9

I have a fancy prompt with colors, and now bash doesn't seem to know how wide my terminal is. Lines wrap around incorrectly. I have another proper way to do this, put this code in your ~/.bashrc or create a new file and source file : PROMPT_COMMAND=$( cat<<-'EOF' retval=$? RED=$(tput setaf 1) GREEN=$(tput setaf 2) STOP=$(tput ...


8

grep and ls have already been mentioned, if you want a lot more colors check out Generic Coloriser, its initial purpose was to colorize logfiles, but right out of the box it also colorizes ping, traceroute, gcc, make, netstat, diff, last, ldap, and cvs. It's easily extended if you know regexes. I've added ps and nmap to the list (if you get into grc I'll be ...


8

basically, you need: trap 'echo -ne "\033]0;$BASH_COMMAND\007"' DEBUG at the end of your .bashrc or similar. Took me a while to work this out -- see my answer here for more information :)


8

My background is RHEL-derived distributions (mainly Fedora, today), Arch is foreign to me. Back when /etc/inittab and mgetty where used by RHEL, you could edit /etc/inittab and pass the -p option to mgetty. This would set the prompt. Now with the adoption of systemd /etc/inittab is empty. The tty configuration file is now ...


8

Bash outputs the prompt only in interactive mode. I.e. it is normally output to the terminal (/dev/tty on linux). That is neither /dev/stdout or /dev/stdin :) Now, I'm not sure but I can imagine that bash will allow limited interactive mode when there isn't a fully functional tty. In that case I'd expect the prompt to be written to stdout. I haven't tested ...


8

As I suspected in my comment, you are missing a \] in your PS1. Try this one: PS1='\[\e]2;\u@\h\a\]\u@\h \W \$ ' For comparison, the original is PS1='\[\e]2;\u@\h\a\u@\h \W \$ ' # ^ missing \]


8

This is controlled by the PS1 environment variable. You can see what this is by running, as root, echo $PS1. You can then set the variable in your own bash profile with echo 'PS1=<value of PS1>' >> ~/.bashrc. You can google for lots of possible values of PS1, but the part you are currently missing is \w for working directory.


8

Take a look at minimodem. minimodem - software audio Bell-type or RTTY FSK modem Copyright (C) 2011 Kamal Mostafa <kamal@whence.com> Minimodem is a command-line program which generates (or decodes) audio modem tones at any specified baud rate, emulating an old Bell-type or radio-teletype FSK modem. The tones can be played to (or recorded from) the ...


8

Let's clean it up a bit and make it more portable. In general, it's best to use tput to generate the control sequences instead of hard coding them, as described in Bash FAQ 53. This way it is much easier to find your missing \]. red=$(tput setaf 1) green=$(tput setaf 2) yellow=$(tput setaf 3) white=$(tput setaf 7) reset=$(tput sgr0) ...


7

This text is printed by your shell, and it's called a prompt. Your shell is probably bash. The prompt is set in bash's initialisation file, which is .bashrc in your home directory. Specifically, the prompt is set by the PS1 variable. Its value can contain escape sequences that are replaced by some value when the prompt is displayed. For example, ...


7

(Inspired by this SU answer) You can combine a couple bash tricks: If you trap a DEBUG signal, the handler is called before each command is executed The variable $BASH_COMMAND holds the currently executing command So, trap DEBUG and have the handler set the title to $BASH_COMMAND: trap 'echo -ne "\033]0;$BASH_COMMAND\007"' DEBUG This will keep the ...


7

Staying in bash (or any other shell if you display the prompt independently): case $answer in [Yy]*) echo Ok;; *) echo "Can't you read? I said to say yes.";; esac This accepts responses like yn as yes,  y (with an initial space) as no, and wlkjzuhfod as no, which may not be optimal but is consistent with typical shell prompts: that's how rm -i, find ...


7

/etc/bashrc is the system setting. The usual place to change the prompt would be the per-user file, ~/.bashrc. You might already have a setting there that's overwriting the system default. Put your prompt string in ~/.bashrc. Furthermore, there is a quirk in bash's handling of initialization files. The /etc/bashrc and ~/.bashrc files are only read for ...


7

Instead of putting it in PROMPT_COMMAND, put it in PS1: PS1='$($HOME/bin/myprompt) \[\033[34m\]\$\[\033[0m\] ' When it's in PS1, bash counts the number of characters that get printed so that it can correctly redraw when you scroll through your history. That's why bash has the \[ and \] special characters—they tell bash that the enclosed characters ...


7

In zsh, you can define the hook function preexec to echo a specified string before every command is executed. In bash, you could use the DEBUG trap to set up a preexec hook to do the same. You would define a preexec() function something like this: preexec () { echo "-------------------------------"; } preexec_invoke_exec () { [ -n "$COMP_LINE" ] ...


7

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


6

I found this via SU. Here's the basic example, though I'm still customizing it for myself: function zle-line-init zle-keymap-select { RPS1="${${KEYMAP/vicmd/-- NORMAL --}/(main|viins)/-- INSERT --}" RPS2=$RPS1 zle reset-prompt } zle -N zle-line-init zle -N zle-keymap-select I'd explain it except I don't really understand it yet



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