When I use "delete key" in some bash terminal, the key sends a tilde "~" instead of deleting the char that is supposed to be deleted.
Find a solution:
1. In your $HOME directory, create or edit your keyboard-mapping file, $HOME/.inputrc
2. Add one mapping line:
"\M-[3~": delete-char
3. restart your terminal
Sunday, April 11, 2010
Linux:bash:about "delete key"
Wednesday, July 1, 2009
Linux:Shell:Bash: how to search slash or backslash by sed
It is tricky to search (or replace) signs slash '\' or backslash '/' by sed. By searching internet, I found 2 approaches:
1. Put the command line of sed to a file called my.sed:
/D\\O/p
(this will print a line containing 'D\O')
2. Another way is to use single quote instead of double one in your shell command line:
sed -n '/D\\O/ p' myfile.txt
(-n is prevent from printing)
Thursday, April 9, 2009
Linux:shell:BASH: How to delete one line by sed
If you want to delete one line containing one word "ThisWord" by sed, then you can do in the way:
sed -i "/ThisWord/d" YourTextFile.txt
Note, here '-i' is to delete it from the original file. Without it, sed only prints the file on screen. So before using '-i', make sure you are doing correct things.
Thursday, December 18, 2008
Linux:shell:Bash: How to change color of your shell printout

You could use 'echo -e' to change your printout text and background color by some color variables. One of the easiest ways to use them is to define shell variable, for example, define those variables in your shell script:
red='\e[0;31m'
RED='\e[1;31m'
blue='\e[0;34m'
BLUE='\e[1;34m'
cyan='\e[0;36m'
CYAN='\e[1;36m'
NC='\e[0m' # No Color
. '\e[0;36m': 0's place is the color of background (0, no effect, 1, bold, 4, underline) and 36 is the text color. You also can change background colors due to color values in the color table. For example, '\e[41;34m' changes background color to red and text color to blue.
How to print?
echo -e "$red These texts are in red$NC" #you must use '-e' to enable interpretation of the backslash-escaped characters, see echo's help.
or if you want to use it in 'printf':
printf "%s\n" "`echo -e $red`These texts are in red`echo -e $NC`"
There is a table of color values attached (this table is from the link). You can find color values what you want.
Saturday, November 8, 2008
linux:shell:bash:grep: multi-grep
If I want to search multi words by grep, I could do in the way:
egrep "a|b|c|d" my_input_file.txt
then egrep will help you to search OR of a, b, c and d. If searching for AND of multi-words, you can do by pipe:
grep a my_input_file.txt |grep b |grep c |grep d
. However, sometimes, it is not so easy to use pipe in one line for the above usage. You can always build string of the command first and evaluate it. For example,
cmd="grep $var1 my_input_file.txt|grep $var2"
...
cmd="$cmd |grep $var3"
...
eval $cmd
Tuesday, July 8, 2008
Shell:Bash:replace substring in string
${string/substring/replacement} Replace first match of $substring with $replacement
${string//substring/replacement} Replace all matches of $substring with $replacement
${string/#substring/replacement} If $substring matches front end of $string, substitute $replacement for $substring
${string/%substring/replacement} If $substring matches back end of $string, substitute $replacement for $substring
Refer to the link for more information.
Tuesday, May 13, 2008
Linux:Bash:how to print quotation marks in awk
ls | awk '{print "'\''"$1"'\''"}'
or
awk 'BEGIN { print "Here is a single quote \47" }'
Friday, July 20, 2007
Linux:shell:grep how to use grep's result
mystr="IloveChina"
echo $mystr |grep China
echo $? #this will return 0
echo $mystr |grep USA
echo $? #this will return 1
#so you could use this returned value to justify if grep found the string.
# Or you use how many searched results returned to do this, for example,
echo $mystr |grep -c China # return 1 to you
echo $mystr |grep -c USA # return 0 to you
# so simply, you can use the number to justify if you found the results like
if [ `echo $mystr |grep -c China` -ge 1 ]; then
echo "Yes, you are"
fi
Tuesday, June 5, 2007
Linux: alias: shell: define your alias in Linux
Except /etc/profile, ~/.bashrc and ~/.bash_profile, you can add alias into the file:
/etc/profile.d/alias.sh (or alias.csh for c shell)
This file is effective for all users like /etc/profile.
Sunday, April 1, 2007
SHELL: add head to a bash script
Every time when you write shell script, it is sick to type the same head on every script? If you are, try this small script to add head to your code.
#!/bin/sh
help(){
echo "Usage: "
echo " `basename $0`
echo " `basename $0` -h "
exit 1
}
if [ $# -lt 1 ] || [[ $1 = -h ]]
then
help
fi
echo " "
echo "Input the title of your code:"
read line
newline[0]='#!/bin/sh'
newline[1]="#######################################"
newline[2]="# $1"
newline[3]="# $line"
newline[4]="# "
newline[5]="# Created by Your name (you@email.com)"
newline[6]="# on `date`"
newline[7]='# Usage: '
newline[8]='# '
newline[9]='# Modifications: '
newline[10]='# '
newline[11]='#######################################'
newline[12]=' '
newline[13]='help() {'
newline[14]=' echo ""'
newline[15]=' echo "Usage: $0 <>"'
newline[16]=' echo " <>"'
newline[17]=' echo " -h or no-args for this help info."'
newline[18]=' echo ""'
newline[19]='}'
newline[20]='if [[ $1 == "-h" ]] || [ $# -lt 1 ]'
newline[21]=' then'
newline[22]=' help'
newline[23]=' exit 0'
newline[24]='fi'
#this total value must be equal to the last index!!!
total=24
index=0
while [ $index -le $total ]
do
((iline=index+1))
sed -i "${iline}i\\${newline[$index]}" $1
((index+=1))
done
SHELL: a very good e-reference about bash
An excellent reference about bash is here. It descripts bash in a very detail. I have some small bash scripts. Later I will post them here for sharing.
