Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Friday 11 March 2022

Git branch in Ubuntu terminal

This is the code I added to ~/.bashrc around line 59. It affects the CLI prompt in the Ubuntu terminal in a number of ways.

  1. It removes the username and computer name.
  2. It adds the open Git branch, if one exists.
  3. It adds a new line at the end, because some path's get very long, and don't give you much room to add your commands. 

parse_git_branch() {

 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'

}


if [ "$color_prompt" = yes ]; then

 PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\[\033[01;34m\]\w\[\033[01;32m\] $(parse_git_branch)\[\033[00m\]\n$ '

else

 PS1='${debian_chroot:+($debian_chroot)} $(parse_git_branch)\n$ '

fi

Friday 6 December 2013

Bash techniques

I've been doing a lot of work in bash lately. I've been speeding up the time taken to create the website basics. Right now I can create a Twitter Bootstrap/Modernizr website with blogs etc within a couple of seconds by calling my bash script. So, below are some techniques I've learned along the way:
Prep-end all slashes in a web address with backslashes
webdir=`echo $webdir | sed s,/,\\\\\\\\\\/,g`
Get the last substring from a string separated by slashes
dbn=${longstring##*/}
Put the contents of a .sql into a database
mysql -u root -p password $dbn < $workingdir"/users.sql"
Change a line within a .ini file
sed -i -e '/DB_NAME =/ s/= .*/= '$dbn'/' config.ini
Hope they help!