Showing posts tagged bash

The trouble with bash colored prompts munging your control-r bash history searches

Basically, you just want to be very careful properly escaping and ending color sequences in your bash prompt.  If you mess them up, when you try to use spiffy bash tricks that mess with your current line, such as control-r to search through your bash history and then left of right arrow to edit that line, you’ll get a partially overwritten line that is impossible to read or edit properly.

 1 2 3 4 5 6 7 8 9 10 11 

#Just be careful. Safest to properly end color sequence, so instead of this
#export PS1=”\[\033[01;32m\]\u\[\033[00m\]\[\033[01;31m\]\$(git_br)\[\033[01;32m\]\[\033[00m\]:\[\033[01;36m\]\$(git_pwd)\[\033[00m\]\$ “
 
#do this
USER_GREEN=’\[\e[01;32m\]’
#NO_COLOR=’\[\e[00m\]’
REPO_RED=’\[\e[01;31m\]’
PATH_BLUE=’\[\e[01;36m\]’
END_COLOR=‘\e[m’
 
PS1=”${USER_GREEN}\u${END_COLOR}${REPO_RED}\$(git_br)${END_COLOR}:${PATH_BLUE}\$(git_pwd)${END_COLOR}$ “

Posted via web from a timocracy of one | Comment »

My Favorite bash Tips and Tricks

which produces the same result. However, brace expansion becomes quite useful when the brace-enclosed list occurs immediately before, after or inside another string:

$ echo {one,two,red,blue}fish  onefish twofish redfish bluefish    $ echo fish{one,two,red,blue}  fishone fishtwo fishred fishblue    $ echo fi{one,two,red,blue}sh  fionesh fitwosh firedsh fibluesh  

Notice that there are no spaces inside the brackets or between the brackets and the adjoining strings. If you include spaces, it breaks things:

$ echo {one, two, red, blue }fish  {one, two, red, blue }fish    $ echo "{one,two,red,blue} fish"  {one,two,red,blue} fish  

However, you can use spaces if they’re enclosed in quotes outside the braces or within an item in the comma-separated list:

$ echo {"one ","two ","red ","blue "}fish  one fish two fish red fish blue fish    $ echo {one,two,red,blue}" fish"  one fish two fish red fish blue fish  

You also can nest braces, but you must use some caution here too:

$ echo {{1,2,3},1,2,3}  1 2 3 1 2 3    $ echo {{1,2,3}1,2,3}  11 21 31 2 3  

Now, after all these examples, you might be thinking to yourself, “Gee, those are great parlor tricks, but why should I care about brace expansion?”

Brace expansion becomes useful when you need to make a backup of a file. This is why it’s my favorite shell trick. I use it almost every day when I need to make a backup of a config file before changing it. For example, if I’m making a change to my Apache configuration, I can do the following and save some typing:

$ cp /etc/httpd/conf/httpd.conf{,.bak}  

Notice that there is no character between the opening brace and the first comma. It’s perfectly acceptable to do this and is useful when adding characters to an existing filename or when one argument is a substring of the other. Then, if I need to see what changes I made later in the day, I use the diff command and reverse the order of the strings inside the braces:

$ diff /etc/httpd/conf/httpd.conf{.bak,}  1050a1051  > # I added this comment earlier  

Brace expansion is pretty cool when used as pseudo-globbing.

Posted via web from a personal timocracy | Comment »