This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
public:techstuff:advancedbash [2012/08/21 13:50] – external edit 127.0.0.1 | public:techstuff:advancedbash [2020/04/25 13:05] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Advanced Bash ====== | ||
+ | |||
+ | ==Summary== | ||
+ | There are a variety possibilities for your bash prompt, and customizing it will help you be more productive at the command line. You can add additional information to your prompt, or you can simply color it to make the prompt stand out. | ||
+ | |||
+ | ==Basic Prompts== | ||
+ | The following settings are useful for distinguishing the root prompt from non-root users: | ||
+ | |||
+ | < | ||
+ | < | ||
+ | Set the above < | ||
+ | |||
+ | < | ||
+ | < | ||
+ | Set the above < | ||
+ | |||
+ | ==Distinct root and non-root Prompts== | ||
+ | Use 2 separate config files; one for root (/ | ||
+ | vim ~/.bashrc | ||
+ | Comment out the default PS1: | ||
+ | # | ||
+ | Use the following prompt for normal user: | ||
+ | | ||
+ | It features username in green, working directory in bright blue, a bright green $ prompt, and bright white text-type. | ||
+ | |||
+ | Edit root prompt: | ||
+ | sudo vim / | ||
+ | Comment out default: | ||
+ | # | ||
+ | Use the following prompt for root: | ||
+ | | ||
+ | |||
+ | It features username ' | ||
+ | |||
+ | Here is the bash color code: | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | Light Gray 0;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. | ||
+ | |||
+ | ''' | ||
+ | | ||
+ | | ||
+ | |||
+ | ==Customize your Bash prompt further== | ||
+ | |||
+ | *Edit your ~/.bashrc by commenting out the default Arch prompt: | ||
+ | |||
+ | # PS1=' | ||
+ | *Add the following entries to your ~/.bashrc | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | The \[ and \] are necessary to not get line-wrapping problems on the terminal. | ||
+ | |||
+ | Then do: | ||
+ | | ||
+ | You may wish to edit your / | ||
+ | |||
+ | |||
+ | |||
+ | the various \x's (ripped from bash manpage): | ||
+ | |||
+ | Bash allows these prompt strings to be customized by inserting a | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | is inserted into the prompt string an empty format | ||
+ | results in a locale-specific time representation. | ||
+ | The braces are required | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | the final slash) | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | abbreviated with a tilde | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | to embed a terminal control sequence into the prompt | ||
+ | | ||
+ | | ||
+ | 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 | ||
+ | | ||
+ | | ||
+ | the description of the shopt command under SHELL BUILTIN COMMANDS | ||
+ | | ||
+ | |||
+ | == Set xterm window title == | ||
+ | Add this code fragment to you .bashrc | ||
+ | < | ||
+ | case " | ||
+ | xterm | xterm-color) | ||
+ | | ||
+ | ;; | ||
+ | | ||
+ | And since there you can use ${XTERM_TITLE} macro at begining of PS1 to set xterm title (if available) to directory@user@hostname. Or you can modify it (XTERM_TITLE=... line) using regular PS1 escape sequencies. | ||
+ | |||
+ | == Positioning the Cursor == | ||
+ | |||
+ | The following sequence sets the cursor position: | ||
+ | |||
+ | | ||
+ | |||
+ | The current cursor position can be saved using | ||
+ | |||
+ | | ||
+ | |||
+ | To restore a position, use the following sequence: | ||
+ | |||
+ | | ||
+ | |||
+ | The following example uses these sequences to display the time in the upper right corner: | ||
+ | |||
+ | | ||
+ | |||
+ | The environment variable COLUMNS contains the number of columns of the terminal. The above example substracts 4 from its value in order to justify the five character wide output of '' | ||
+ | |||
+ | == List of colors for prompt and bash == | ||
+ | Add this to your .bashrc to use colors in prompt and commands: | ||
+ | |||
+ | < | ||
+ | TXTRED=' | ||
+ | TXTGRN=' | ||
+ | TXTYLW=' | ||
+ | TXTBLU=' | ||
+ | TXTPUR=' | ||
+ | TXTCYN=' | ||
+ | TXTWHT=' | ||
+ | BLDBLK=' | ||
+ | BLDRED=' | ||
+ | BLDGRN=' | ||
+ | BLDYLW=' | ||
+ | BLDBLU=' | ||
+ | BLDPUR=' | ||
+ | BLDCYN=' | ||
+ | BLDWHT=' | ||
+ | UNDBLK=' | ||
+ | UNDRED=' | ||
+ | UNDGRN=' | ||
+ | UNDYLW=' | ||
+ | UNDBLU=' | ||
+ | UNDPUR=' | ||
+ | UNDCYN=' | ||
+ | UNDWHT=' | ||
+ | BAKBLK=' | ||
+ | BAKRED=' | ||
+ | BAKGRN=' | ||
+ | BAKYLW=' | ||
+ | BAKBLU=' | ||
+ | BAKPUR=' | ||
+ | BAKCYN=' | ||
+ | BAKWHT=' | ||
+ | TXTRST=' | ||
+ | |||
+ | Use in commands (use curly brackets only if you need to specify text right after value ''' | ||
+ | | ||
+ | < | ||
+ | | ||
+ | < | ||
+ | | ||
+ | < | ||
+ | | ||
+ | < | ||
+ | |||
+ | Use in prompt (note double quotes and < | ||
+ | | ||
+ | |||
+ | == Return value visualisation == | ||
+ | You can add this line to the end of your .bashrc if you want to see return value of last executed command. This should work with any kind of prompt as long as it don't need PROMPT_COMMAND. | ||
+ | < | ||
+ | |||
+ | It will look like this: | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | Zero is green and non-zero is red. There is also smiley (you can replace it with anything what you want). So your bash will smile if last operation was succesful. | ||
+ | |||
+ | == Better return value visualisation == | ||
+ | If you want colors, you need to set $RED and $GREEN values: | ||
+ | | ||
+ | | ||
+ | |||
+ | You have to specify this values in [[public: | ||
+ | < | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | Then you can use $RET_VALUE and $RET_SMILEY macros in PS1 (prompt). Note that you need use double quotes: | ||
+ | # | ||
+ | | ||
+ | |||
+ | This will give you basic prompt : | ||
+ | 0 <font color=" | ||
+ | 0 <font color=" | ||
+ | 1 <font color=" | ||
+ | |||
+ | But you will probably want to use $RET_VALUE or $RET_SMILEY in your own prompt. like me: | ||
+ | < | ||
+ | |||
+ | ==Advanced Prompts== | ||
+ | ===Wolfman' | ||
+ | After reading through most of the [http:// | ||
+ | |||
+ | *Comment out Arch's default prompt. | ||
+ | # PS1=' | ||
+ | |||
+ | *Next add the bash_prompt_command function. If you have a couple directories with long names or start entering a lot of subdirectories, | ||
+ | ################################################## | ||
+ | # 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 | ||
+ | # / | ||
+ | # / | ||
+ | ################################################## | ||
+ | | ||
+ | # 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## | ||
+ | | ||
+ | | ||
+ | local pwdoffset=$(( ${#NEW_PWD} - pwdmaxlen )) | ||
+ | if [ ${pwdoffset} -gt " | ||
+ | then | ||
+ | | ||
+ | | ||
+ | fi | ||
+ | } | ||
+ | |||
+ | *This code generates the command prompt. There' | ||
+ | *Also, make sure that your color variables are enclosed in double and not single quote marks. | ||
+ | | ||
+ | case $TERM in | ||
+ | xterm*|rxvt*) | ||
+ | local TITLEBAR=' | ||
+ | ;; | ||
+ | *) | ||
+ | local TITLEBAR="" | ||
+ | ;; | ||
+ | esac | ||
+ | local NONE=" | ||
+ | |||
+ | # regular colors | ||
+ | local K=" | ||
+ | local R=" | ||
+ | local G=" | ||
+ | local Y=" | ||
+ | local B=" | ||
+ | local M=" | ||
+ | local C=" | ||
+ | local W=" | ||
+ | |||
+ | # emphasized (bolded) colors | ||
+ | local EMK=" | ||
+ | local EMR=" | ||
+ | local EMG=" | ||
+ | local EMY=" | ||
+ | local EMB=" | ||
+ | local EMM=" | ||
+ | local EMC=" | ||
+ | local EMW=" | ||
+ | |||
+ | # background colors | ||
+ | local BGK=" | ||
+ | local BGR=" | ||
+ | local BGG=" | ||
+ | local BGY=" | ||
+ | local BGB=" | ||
+ | local BGM=" | ||
+ | local BGC=" | ||
+ | local BGW=" | ||
+ | |||
+ | local UC=$W # user's color | ||
+ | [ $UID -eq " | ||
+ | |||
+ | | ||
+ | # without colors: PS1=" | ||
+ | # extra backslash in front of \$ to make bash colorize the prompt | ||
+ | } | ||
+ | |||
+ | *Finally, append this code. This ensures that the NEW_PWD variable will be updated when you cd somewhere else, and it sets the PS1 variable, which contains the command prompt. | ||
+ | | ||
+ | | ||
+ | unset bash_prompt | ||
+ | |||
+ | *If you want to play around with the colors of this prompt, open your < | ||
+ | $ source ~/.bashrc |