Bash hints to know or how to make complex issues simple
- Detailed documentation for sed doc_sed.odt
- Detailed documentation for vim doc_vim.odt
#!/bin/bash echo {1,2,3,4,5}{a,b,c,d,e,f} # result : 1a 1b 1c 1d 1e 1f 2a 2b 2c 2d 2e 2f 3a 3b 3c 3d 3e 3f 4a 4b 4c 4d 4e 4f 5a 5b 5c 5d 5e 5f
#!/bin/bash I=foobar #trailing substitution echo ${I%bar} # result :foo #leading substitution echo ${I#foo} # result :bar #full substitution STR="the wall is black" echo ${STR/black/blue} # result : the wall is blue
#!/bin/bash F="hello HELLO" echo ${F^^} # result : HELLO HELLO echo ${F,,} # result :hello hello
#!/bin/bash F="abcdefgh" echo ${F:5:3} # result : fgh echo ${#F} # result : 8
#!/bin/bash F="my_file.log" echo ${F%.log}.txt # result : my_file.txt
#!/bin/bash for F in `find . -name "*.java"` ; do echo $F # rename .java to .txt mv $F ${F%.java}.txt done
#!/bin/bash # list of arguments passed to script as string echo $* # list of arguments passed to script as delimited list echo $@ #number of arguments passed to current script echo $# # name of shell script (relative path from the current directory) echo $0 # argument 1 echo $1 #pid of the current shell echo $$ bash -c "exit 24" #last exit status echo $? #PID of the most recent background ( empty if not set) echo "PID :"$! cat /proc/cpuinfo >/dev/null & echo "PID :"$! #the (input) field separator echo -n "$IFS" | od -abc SET="1 2 3 4 5" for I in $SET ; do echo "I="$I ;done # result : #I=1 #I=2 #I=3 #I=4 #I=5 IFS=':' echo -n "$IFS" | od -abc for I in $SET ; do echo "I="$I ;done # result : #I=1 2 3 4 5
touch 'test test' IFS=$'\n' for F in `find . -name "* *"` ; do OLDF=`echo $F | sed -e 's# #\ #g'` echo $OLDF NEWF=`echo $F | sed -e 's/ /_/g'` echo $NEWF mv $OLDF $NEWF done