Jugando con el prompt de la terminal


Estos dias he estado cambiando el prompt de mi terminal, la verdad es que nunca lo había hecho O.o pero está bastante bien, para quien no lo sepa, el prompt es lo que está escrito antes de los comandos en la terminal.

Os dejo algunos de los que me he puesto:

-> PS1="\n\[\e[30;1m\]\[\016\]\[\017\](\[\e[34;1m\]\u@\h\[\e[30;1m\])-(\[\e[34;1m\]\@ \d\[\e[30;1m\])\n(\[\e[32;1m\]\w\[\e[30;1m\])-(\[\e[32;1m\]\$(/bin/ls -1 | /usr/bin/wc -l | /bin/sed 's: ::g') files, \$(/bin/ls -lah | /bin/grep -m 1 total | /bin/sed 's/total //')b\[\e[30;1m\])-> \[\e[0m\]"
minimal:
v1.
-> PS1="\[\e[30;1m\]\[\016\]\[\017\](\[\e[32;1m\]\W\[\e[30;1m\])\`if [ \$? = 0 ]; then echo '·'; else echo '\[\e[31m\]\041\[\e[0m\]'; fi\`\[\e[30;1m\]> \e[35;1m"
v2.
-> PS1="\[\e[30;1m\]\[\016\]\[\017\](\[\e[32;1m\]\W\[\e[30;1m\])\`if [ \$? = 0 ]; then echo '·>'; else echo '\[\e[31m\]·>\[\e[0m\]'; fi\` \e[35;1m"
other:
-> PS1="\[\e[30;1m\]\[\016\]\[\017\](\[\e[32;1m\]\W\[\e[30;1m\])-(\[\e[32;1m\]\$(/bin/ls -1 | /usr/bin/wc -l | /bin/sed 's: ::g') files, \$(/bin/ls -lah | /bin/grep -m 1 total | /bin/sed 's/total //')b\[\e[30;1m\])-> \[\e[0m\]\e[1;36m"
-> PS1="\[\e[30;1m\]\[\016\]\[\017\](\[\e[32;1m\]\w\[\e[30;1m\])\n\[\e[32;1m\]\u\[\e[30;1m\] {\[\e[32;1m\]\A,\$(/bin/ls -lah | /bin/grep -m 1 total | /bin/sed 's/total //')b\[\e[30;1m\]} \[\e[0m\]\e[1;37m"

Aquí una captura con algunos de los que me puse, hay uno que lo cambié pero no recuerdo cual era :S


Para probar los prompts solo tienen que ejecutar el comando, básicamente lo que hace es asignar un valor a 'PS1' que es la variable del prompt. Por supuesto, en otras terminales no se vería el prompt ya que el valor de 'PS1' se ha cambiado localmente, para cambiarlo en todas las terminales tienen que ponerlo en el fichero '.bashrc' de la carpeta de usuario(cada usuario puede tener un prompt diferente, incluyendo el root).

Obviamente la variable ya vendrá definida, la sobreescriben y ya. Aquí os dejo los códigos que he encontrado para poner en el prompt:
\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 part
\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 character
Bueno, con eso podéis hacer un prompt decentillo, pero también podéis añadirle color a la cosa :D, la sintaxis es la siguiente:
 \e[     - principio
x;ym   - código del color
\e[m   - fin

Y estos son los códigos de colores (si cambian el '0;' por un '1;' es para fondos negros, osea que los colores son mas blancos, de hecho el marrón se convierte en amarillo jaja):
negro 0;30
azul 0;34
verde 0;32
cyan 0;36
rojo 0;31
morado 0;35
marrón 0;33
La cosa está en combinar colores y códigos para personalizar el prompt, yo os recomiendo poner los colores de esta forma, a mi me resulta bastante mas fácil:
black='\[\e[1;30m\]'
blue='\[\e[1;34m\]'
green='\[\e[1;32m\]'
cyan='\[\e[1;36m\]'
red='\[\e[1;31m\]'
purple='\[\e[1;35m\]'
brown='\[\e[1;33m\]'
endcolor='\[\e[0m\]'
Simplemente son los colores encapsulados entre '\[' y '\]' para que estén mas ordenados, así ya tienen las variables del color definidas y no hay tanto lío en la variable 'PS1' jeje. Espero que les haya servido, compartan cualquier prompt que hagan :)