Color Bash Prompt (Italiano)
Template:I18n entry Template:I18n entry Template:I18n links end
Contents
Sommario
Esistono varie possibilità per il proprio prompt di bash e personalizzarlo aiuterà ad essere più produttivi dalla linea di comando. Si possono aggiungere informazioni al proprio prompt, o semplicemente colorarlo per farlo risaltare.
Prompt basilari
Le seguenti impostazioni sono utili per distinguere il prompt di root da quello di utente normale:
Verde per utenti normali:
export PS1='\[\e[0;32m\]\u@\h \W]\$ '
Impostare la variabile PS1
nel proprio file ~/.bashrc
ed esportarla.
Rosso per root:
export PS1='\[\e[0;31m\]\u@\h \W]\$ '
Impostare la variabile PS1
nel proprio file /root/.bashrc
ed esportarla.
Misfit138's Prompts
I use 2 separate ~/.bashrc entries; one for root, and one for normal user:
vim ~/.bashrc
Comment out the default PS1:
#PS1='[\u@\h \W]\$ '
I use the following prompt for normal user:
PS1='\[\e[0;32m\]\u\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \[\e[1;32m\]\$ \[\e[m\]\[\e[1;37m\] '
It features username in green, working directory in bright blue, a bright green $ prompt, and bright white text-type.
Edit root prompt:
sudo vim /root/.bashrc
Comment out default:
#PS1='[\u@\h \W]\$ '
Use the following prompt for root:
PS1='\[\e[0;31m\]\u\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \[\e[0;31m\]\$ \[\e[m\]\[\e[0;32m\] '
It features username 'root' in red, working directory in bright blue, a red '#' for su designation, and text-typing in a nasty, matrix-like, Type-O-Negative green. ;)
Here is the bash color code:
Black 0;30 Dark Gray 1;30 Blue 0;34 Light Blue 1;34 Green 0;32 Light Green 1;32 Cyan 0;36 Light Cyan 1;36 Red 0;31 Light Red 1;31 Purple 0;35 Light Purple 1;35 Brown 0;33 Yellow 1;33 Light Gray 0;37 White 1;37
Replace digit 0 with 1 to get bright color version! Experiment as desired, and see bash manpage excerpt below to further customize backslash-escaped special characters.
Tip:
\[ begins a sequence of non-printing characters \] ends a sequence of non-printing characters
Customizzare maggiormente il prompt di Bash
- Editare il proprio ~/.bashrc e commentate il prompt di default di Arch:
# PS1='[\u@\h \W]\$ '
- Aggiungete le seguenti righe al vostro ~/.bashrc
BLUE=`tput setf 1` GREEN=`tput setf 2` CYAN=`tput setf 3` RED=`tput setf 4` MAGENTA=`tput setf 5` YELLOW=`tput setf 6` WHITE=`tput setf 7` PS1='\[$GREEN\]\u@\h \[$BLUE\]\w/\[$GREEN\] \$\[$WHITE\] '
i caratteri \[
e \]
sono necessari per evitare diverse interpretazione di parte della shell.
Dopo di che diamo:
source ~/.bashrc
Potreste editare anche il file /root/.bashrc aggiungendo cose simili, magari con RED al posto di GREEN sulla \u
.
le varie \u \h \w possono essere trovate nella pagina di manuale di bash:
man bash
Bash allows these prompt strings to be customized by inserting a number of backslash-escaped special characters that are decoded as follows: \a an ASCII bell character (07) \d the date in "Weekday Month Date" format (e.g., "Tue May 26") \D{format} the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required \e an ASCII escape character (033) \h the hostname up to the first `.' \H the hostname \j the number of jobs currently managed by the shell \l the basename of the shell's terminal device name \n newline \r carriage return \s the name of the shell, the basename of $0 (the portion following the final slash) \t the current time in 24-hour HH:MM:SS format \T the current time in 12-hour HH:MM:SS format \@ the current time in 12-hour am/pm format \A the current time in 24-hour HH:MM format \u the username of the current user \v the version of bash (e.g., 2.00) \V the release of bash, version + patch level (e.g., 2.00.0) \w the current working directory, with $HOME abbreviated with a tilde \W the basename of the current working directory, with $HOME abbreviated with a tilde \! the history number of this command \# the command number of this command \$ if the effective UID is 0, a #, otherwise a $ \nnn the character corresponding to the octal number nnn \\ a backslash \[ begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt \] end a sequence of non-printing characters The command number and the history number are usually different: the history number of a command is its position in the history list, which may include commands restored from the history file (see HISTORY below), while the command number is the position in the sequence of commands executed during the current shell session. After the string is decoded, it is expanded via parameter expansion, command substitution, arithmetic expansion, and quote removal, subject to the value of the promptvars shell option (see the description of the shopt command under SHELL BUILTIN COMMANDS below).
Prompt avanzati
Wolfman's
Wolfman ha sviluppato in prompt colorato che visualizza gli ultimi 25 caratteri della directory in cui vi trovate. Questo tipo di prompt si usa bene in un terminale con uno sfondo nero.
Per usarlo il codice deve essere inserito nel file .bashrc
che si trova nella vostra home.
- Commentiamo il prompt di default di Arch.
# PS1='[\u@\h \W]\$ '
- Aggiungiamo quindi la funzione bash_prompt_command. Se avete directory con nomi lunghi o con un enorme quantità di sotto directory, questa funzione evita che il prompt diventi troppo lungo. Questo codice è stato preso dalla sezione Bash Prompt Howto's di Controlling the Size and Appearance of $PWD e modificato per rimpiazzare la home dell'utente con una tilde.
################################################## # Fancy PWD display function ################################################## # The home directory (HOME) is replaced with a ~ # The last pwdmaxlen characters of the PWD are displayed # Leading partial directory names are striped off # /home/me/stuff -> ~/stuff if USER=me # /usr/share/big_dir_name -> ../share/big_dir_name if pwdmaxlen=20 ################################################## bash_prompt_command() { # How many characters of the $PWD should be kept local pwdmaxlen=25 # Indicate that there has been dir truncation local trunc_symbol=".." local dir=${PWD##*/} pwdmaxlen=$(( ( pwdmaxlen < ${#dir} ) ? ${#dir} : pwdmaxlen )) NEW_PWD=${PWD/#$HOME/\~} local pwdoffset=$(( ${#NEW_PWD} - pwdmaxlen )) if [ ${pwdoffset} -gt "0" ] then NEW_PWD=${NEW_PWD:$pwdoffset:$pwdmaxlen} NEW_PWD=${trunc_symbol}/${NEW_PWD#*/} fi }
- Questo codice genera il prompt dei comandi. Definisce una manciata di colori, il colore dell'username, dell'hostname, e del prompt ($ o #) sono settati a cyano, e se l'utente è root (l'UID di root è sempre 0), setta il colore a rosso. Il prompt dei comandi è settato a una versione colorata del prompt dei comandi di default di Arch tramite NEW_PWD nell'ultima funzione.
- Assicuratevi che le vostre variabili dei colori siano racchiusi tra gli apici doppi e non singoli. Usando gli apici singoli, bash potrebbe dare problemi.
bash_prompt() { case $TERM in xterm*|rxvt*) local TITLEBAR='\[\033]0;\u:${NEW_PWD}\007\]' ;; *) local TITLEBAR="" ;; esac local NONE="\[\033[0m\]" # unsets color to term's fg color # regular colors local K="\[\033[0;30m\]" # black local R="\[\033[0;31m\]" # red local G="\[\033[0;32m\]" # green local Y="\[\033[0;33m\]" # yellow local B="\[\033[0;34m\]" # blue local M="\[\033[0;35m\]" # magenta local C="\[\033[0;36m\]" # cyan local W="\[\033[0;37m\]" # white # emphasized (bolded) colors local EMK="\[\033[1;30m\]" local EMR="\[\033[1;31m\]" local EMG="\[\033[1;32m\]" local EMY="\[\033[1;33m\]" local EMB="\[\033[1;34m\]" local EMM="\[\033[1;35m\]" local EMC="\[\033[1;36m\]" local EMW="\[\033[1;37m\]" # background colors local BGK="\[\033[40m\]" local BGR="\[\033[41m\]" local BGG="\[\033[42m\]" local BGY="\[\033[43m\]" local BGB="\[\033[44m\]" local BGM="\[\033[45m\]" local BGC="\[\033[46m\]" local BGW="\[\033[47m\]" local UC=$W # user's color [ $UID -eq "0" ] && UC=$R # root's color PS1="$TITLEBAR ${EMK}[${UC}\u${EMK}@${UC}\h ${EMB}\${NEW_PWD}${EMK}]${UC}\\$ ${NONE}" # without colors: PS1="[\u@\h \${NEW_PWD}]\\$ " # extra backslash in front of \$ to make bash colorize the prompt }
- Infine, aggiungete questo codice al file .bashrc. questo assicura che la variabile NEW_PWD venga aggiornata quando cambiate directory e inoltre setta la variabile PS1.
PROMPT_COMMAND=bash_prompt_command bash_prompt unset bash_prompt
- Se volete provare varie configurazione dei colori, aprite il file
~/.bashrc
con un editor di testi e quando vorrete vedere come il prompt è cambiato basterà digitare da un prompt qualsiasi.
$ source ~/.bashrc