Using Curly Braces

create a backup of a file

cp file.txt{,.bak}

move a file in a long directory

mv /a/really/really/long/directory/path/{old_file,new_file}

Set command prompt to display current GIT banch

Add to .bash_profile

# set git branch into the prompt
export PS1='[\[$(tput bold)\]\[email protected]\h\[$(tput bold)\] \[$(tput setaf 2)\]`whichBranch` \[$(tput sgr0)\] \w] \n'

create in ~/bin/whichBranch

G=`git branch 2>&1 | head -1 | sed -e 's/* //'`
NB=`echo $G | grep "Not a git repository" | wc -l`
if [ $NB -eq 1 ]; then
   echo "nab"
   #echo $G
   #if [`${#G}` -gt 15]; then
      echo "*"${G:${#G} - 14}

Use the history to run commands

  • list the recent commands
history 5
  • list the recent commands, but limit to the last 5
  • (bang 235) execute command #235 from the history list
  • (bang bang) execute the last command again. Handy to run a command again that you forgot to sudo for (sudo !!)

for loop

for VARIABLE in file1 file2 file3
	command1 on $VARIABLE

infinite loop

while true; do echo 'Hit CTRL+C'; sleep 1; done

recall arguments from previous commands

  • Recall second argument
  • Recall first 3 arguments
  • Ex:
    • echo a b c d e f
    • echo !:2-4 - Would run “echo b c d”