Overview
A very popular shellshell for Unix and Linux is called bashbash, which is kind of short for Bourne-again shell, as it was a replacement for an early shell called the BourneBourne shell, written by Stephen Bourne. It is the default shell for the Unix-based MacOS XMacOS X and most LinuxLinux distributionsdistributions. It is an extremely powerful shell. Earlier shells that influenced bash are the Korn shell (ksh)Korn shell (ksh), the C shell (csh)C shell (csh), and the improved C shell (tcsh)improved C shell (tcsh). Shells abound in the Unix-based world.Short Reference
The first place to get information on any external command (program) used in bash is to use the switch --help on the program:
This gives a short synopsis of what the program does and how some of the major command line switches can be used. To get the full help for a program, you would consult the man pagesman pages. The simplest way is to type:user@host:~> program --help
You may need to specify the section if there are ambiguities:user@host:~> man program
Sections of the man pages:man 1 mkdir man 2 mkdir man 1 printf man 3 printf
You can use the -k option (equivalent to the apropos command) to get a list of related topics, for example:1 Executable programs or shell commands 2 System calls (functions provided by the kernel) 3 Library calls (functions within program libraries) 4 Special files (usually found in /dev) 5 File formats and conventions eg /etc/passwd 6 Games 7 Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7) 8 System administration commands (usually only for root) 9 Kernel routines [Non standard]
Displays:man -k mkdir
These two techniques (man and --help) will only work on external commands, i.e. programs. To get help on built-in commands, use the help command:mkdir (1) - make directories mkdir (2) - create a directory mkdirat (2) - create a directory relative to a directory file descriptor
Some examples:help <built-in command>
To discover whether or not a command is a built-in command or external command, use the type built-in command:help cd help type help help
$ type cd $ cd is a shell builtin
$ type diff $ diff is /usr/bin/diff
$ type ls $ ls is aliased to `ls --color=auto'
$ type dir $ dir is aliased to `ls -l --color --group-directories-first'
$ type foo $ bash: type: foo: not found
Commands are hashed for increased performance. See the hash command for details.$ type sudo $ sudo is hashed (/usr/bin/sudo)
The shaded commands are external commands. All others are internal, although some can be both.
A brief list of other useful commands and programs:
bash Command Similar
Windows CommandDescription chmod attrib.exe Change a file's mode (attributes) cat type Display a text file (also concatenates files) cd cd Change directory clear cls Clears the screen cp copy Copy files date date/time Read/set the system date and time echo* echo Print to the screen for for Looping construct if if Conditional construct less more.com Display one page of text at a time. ls dir List files man help.exe Displays the man (manual) page for a given command mkdir mkdir/md Create (make) a new directory mv move/rename Move and/or rename files popd popd Pop most recent directory and change to it pushd pushd Save current directory and change rm del Removes (deletes) a file rmdir rmdir/rd Removes (deletes) a directory
*These commands also have external equivalents that are used by other shells (e.g. csh)
Command Description alias Create an alias (shortcut) for a command cal Display a calendar to the screen (text) chown Change owner and group of a file dd Data dump, raw read/write for files and devices diff Display differences between files du Calculate disk usage file Determine a files type find Search for files matching certain criteria grep Search files for lines matching specified text head Display first few lines in a file history Display command history kill* Send a signal to a process ln Create a link between files ps List processes and status pwd* Print current working directory tail Show last few lines of a file time* Measure the running time of a program (External command as well) type Determine the type of a command (e.g. file, alias, keyword, built-in, etc.) wc Prints count of lines, words, and bytes which Shows where a program is in the file system
To create a PDF from the man pages, use this command (showing chmod as an example):
For easier use, create a script called man2pdf.sh and put this text in it:man -t chmod > chmod.ps && ps2pdf chmod.ps && rm chmod.ps
#!/bin/bash
if [[ $# -eq 0 ]]; then
echo "Usage: man2pdf topic"
exit
fi
man -t $1 > $1.ps && ps2pdf $1.ps && rm $1.ps
Make sure the script is executable and in your path somewhere (e.g. ~/bin):
chmod +x man2pdf.sh
Redirection, Piping and Filters (Review)
Recall from CS 120:
Here's a couple of very simple examples that show how filters work. We'll use this file: Declaration of Independence without word-wrap. There are actually 9 lines in the file, and 4 of them are empty.
Output:cat doi-nowrap.txt | wc
9 664 3980
cat doi-nowrap.txt | fmt -w 60 > doi-wrap60.txtOutput file: doi-wrap60.txt
cat doi-nowrap.txt | fmt -w 60 | nl -ba > doi-wrap60-nl.txtOutput file: doi-wrap60-nl.txt
$ ifconfig | grep "inet addr" | grep -v "127.0.0.1" | cut -c 21- | cut -d' ' -f1 | sort -n
Now we can get the input from the file ip.txt:$ ifconfig > ip.txt
or$ grep "inet addr" < ip.txt | grep -v "127.0.0.1" | cut -c 21- | cut -d' ' -f1 | sort -n
$ cat ip.txt | grep "inet addr" | grep -v "127.0.0.1" | cut -c 21- | cut -d' ' -f1 | sort -n
The output is:$ cat ip.txt | grep "IP Address" | cut -c 45- | cut -d. -f1,2 | sort | uniq
10.10 192.168
The output is:$ cat ip.txt | grep "IP Address" | cut -c 45- | cut -d. -f1,2 | sort | uniq | wc -l
2
The output is (there are 8 leading spaces, shown with dots ········ to the left):$ cat ip.txt | grep "IP Address"
········IP Address. . . . . . . . . . . . : 192.168.17.1 ········IP Address. . . . . . . . . . . . : 192.168.13.1 ········IP Address. . . . . . . . . . . . : 10.10.32.67 ········IP Address. . . . . . . . . . . . : 192.168.56.1
The output is:$ cat ip.txt | grep "IP Address" | cut -c 45-
192.168.17.1 192.168.13.1 10.10.32.67 192.168.56.1
The output is:$ cat ip.txt | grep "IP Address" | cut -c 45- | cut -d. -f1,2
192.168 192.168 10.10 192.168
The output is:$ cat ip.txt | grep "IP Address" | cut -c 45- | cut -d. -f1,2 | sort
10.10 192.168 192.168 192.168
The output is:$ cat ip.txt | grep "IP Address" | cut -c 45- | cut -d. -f1,2 | sort | uniq
10.10 192.168
The output is:$ cat ip.txt | grep "IP Address" | cut -c 45- | cut -d. -f1,2 | sort | uniq | wc -l
All of the filters are configurable via command line options:2
By the way, the script above to create PDF documents from the man pages could have been done using pipes:
man -t grep | ps2pdf - > grep.pdf
And that, boys and girls, is what makes the command line indispensible for the experienced user. Imagine if you had to write a C/C++ program to do this; it would be quite lengthy. (It's not just the parsing, but you'd have to figure out a system-independent way to query the system for IP addresses, etc.)
One of the best things you can do as a student learning the power of computers is to learn the filters in /usr/bin.
There are hundreds and learning all of them is unlikely. However, you only really need to learn a
couple of dozen to make your computing significantly more productive.
Over time, you will find yourself spending more
and more time at the command line because it enables you to do things that are impossible in a GUI. Also,
before you sit down to write a program in C or C++ to do some kind of system work, find out if there is
a program or set of filters that will do what you want. Most likely, there is, or at
least you may only need to write another filter to complete the task.
Writing Your Own Filters
FILE *infile initialize counters; IF filename given on command line infile = open file for read/text (Handle errors) ELSE infile = stdin; END IF WHILE not end of file read a character inc char_counter IF this is the first char read or the previous character is a newline print line number inc line_counter END IF print the character that was read set prev char to character read END WHILE IF not reading from stdin close file END IF
Simple bash Programming
Variables in bash:
Arithmetic in bash seems a little strange at first. Examples will illustrate. Example 1 - Using the let command.
Code Output Description a=1+2 echo $a let a=1+2 echo $a let a = 1 + 2 echo $a let a="1 + 2" echo $a 1+2 3 error 3No arithmetic done. Verbatim assignment.
The let command is for arithmetic.
Can't have any spaces with let.
Using quotes allows spaces.Example 2 - Using (( )) for arithmetic context.
Code Output Description a=1 ((a=$a+2)) echo $a ((a = a + 2)) echo $a ((a += 2)) echo $a 3 5 7Set a to 1
Add 2 to a
Same, but can use spaces and no $.
Same.
Functions in bash do not specify parameters nor return type, although both are supported.function_name() { command1... command2... ... }
Function | Calling it | Output |
---|---|---|
say_hello() { echo "Hello!" } |
say_hello |
Hello! |
Function | Calling it | Output |
---|---|---|
print3() { echo "Printing: $1 $2 $3" } |
print3 apple banana cherry print3 apple "banana cherry" dog |
apple banana cherry apple banana cherry dog |
Function | Calling it | Output |
---|---|---|
count_files() { local count=`ls -l | wc -l` echo $count } |
num_files=$(count_files) echo $num_files echo "The number of files is $(count_files)" |
17 The number of files is 17 |
Function | Calling it | Output |
---|---|---|
multiply() { local product=$(($1 * $2)) echo $product } |
total=$(multiply 3 5) echo $total |
15 |
if CONDITION-COMMANDS then COMMAND(S)... elif CONDITION-COMMANDS # optional then COMMAND(S)... else # optional COMMAND(S)... fi # required
if CONDITION-COMMANDS; then COMMAND(S)...; fi
Code | Output |
---|---|
value=3 if [[ $value -eq 3 ]]; then echo "The value is 3" else echo "The value is NOT 3" fi |
The value is 3 |
Bash operator Equivalent
C operatorMeaning -eq = equal -ne != not equal -lt < less -gt > greater -le <= less or equal -ge >= greater or equal
Code | Output |
---|---|
string1="Hello" string2="hello" if [[ $string1 == $string2 ]]; then echo "Same strings" else echo "Different strings" fi |
Different strings |
Operator | Meaning |
---|---|
== | equal |
!= | not equal |
< | less |
> | greater |
Code | Output |
---|---|
filename="foo.txt" echo -n "$filename " if [[ -e $filename ]]; then echo "exists" else echo "does not exist" fi |
foo.txt does not exist |
validate() { if [[ -x $1 ]]; then true else false fi }
Code | Output |
---|---|
filename="a.out" echo -n "$filename " if validate $filename; then echo "is executable" else echo "is not executable" fi |
a.out is executable |
Note:
What's the deal with the single and the double brackets? ([ ] vs. [[ ]]). The best answer I've found on the subject is here.
In a nutshell, [ ] is older and more portable, while [[ ]] is newer, easier to use, but doesn't work with all shells. Since we are talking about the bash shell, this will always work. By the way, there is actually a program in /usr/bin that is named [. That's right, do this:
and you will see something like this:ls -l /usr/bin/[
-rwxr-xr-x 1 root root 30,244 2011-02-23 05:22 /usr/bin/[
There are for loops and while loops of course.
Block-style for loop (CS 180 style): for arg in LIST do command(s)... doneOne line needs semicolons: for arg in LIST; do command(s)...; doneExample 1 - These all do the same thing
1 2 3 4 for a in 1 2 3 4 5 do echo -n "$a " done NUMS="1 2 3 4 5" for a in $NUMS do echo -n "$a " done for a in {1..5} do echo -n "$a " done for ((a = 1; a <= 5; a++)) do echo -n "$a " doneOutput:
1 2 3 4 5Note that these two lines mean different things:The first contains 5 separate items (numbers), but the second is a single item (a string). Notice in the second example above, the string "1 2 3 4 5" is assigned to a variable, NUMS, and that variable is used in the for loop.for a in 1 2 3 4 5 for a in "1 2 3 4 5"
Example 2 - This lists all of the .cpp files in the current directory for a in *.cpp do echo "$a " doneExample 3 - Calling a function in the for loop generate_list() { echo "one two three" }for word in $(generate_list) do echo "$word" doneBlock-style while loop (CS 180 style): while CONTROL-COMMAND do COMMAND(S)... doneOne line needs semicolons: while CONTROL-COMMAND; do COMMAND(S)...; doneExample 4 - Block-style while loop example
i=0 while [ $i -lt 4 ] do i=$(($i + 1)) # can also use ((i++)) echo -n "$i " doneExample 5 - Calling a function in the while loop t=0 condition() { ((t++)) if [ $t -lt 5 ] then return 0 # true else return 1 # false fi } while condition do echo "Still going: t = $t" done
list=()
num=5 for ((a = 0; a < num; a++)) do list[(($a))]=$a done
num=5 for ((a = 0; a < num; a++)) do list+=($a) done
for ((a = 1; a < num; a++)) do echo ${list[a]} done
for i in "${list[@]}" do echo $i done
Simple (no loop) | Loop with $@ | Loop with "$@" | Loop with $* | Loop with "$*" |
---|---|---|---|---|
echo $# echo $@ echo $1 echo $2 echo $3 echo $4 echo $5 echo $6 Output: 6 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 |
for a in $@ do echo $a done Output: 1 2 3 4 5 6 7 8 |
for a in "$@" do echo $a done Output: 1 2 3 4 5 6 7 8 |
for a in $* do echo $a done Output: 1 2 3 4 5 6 7 8 |
for a in "$*" do echo $a done Output: 1 2 3 4 5 6 7 8 |
Loop with shift | Copy to an array | Printing the array elements | Another way to print |
---|---|---|---|
num=$# for ((a = 1; a <= num ; a++)) do echo $1 shift done Output: 1 2 3 4 5 6 7 8 |
list=() num=$# for ((a = 1; a <= num ; a++)) do list[(($a))]=$1 shift done |
for ((a = 1; a <= num ; a++)) do echo ${list[a]} done Output: 1 2 3 4 5 6 7 8 |
for i in "${list[@]}" do echo $i done Output: 1 2 3 4 5 6 7 8 |
Output:$ date
Thu May 24 10:21:40 PDT 2012
Command Output ----------------------------------------------------------- date Thu May 24 10:24:11 PDT 2012 dt=$(date) echo dt dt echo $dt Thu May 24 10:24:11 PDT 2012 dt=`date` echo $dt Thu May 24 10:24:11 PDT 2012 $(date) ./script3: line 42: Thu: command not found
bash_script used to produce all of the examples. It ain't pretty, but here it is. There are actually some lines in the script that aren't on this web page. Examples of looping over filenames in a directory.
This is an excellent place to start: Bash Guide for Beginners
A real world example that I use on student submissions.
Enhancing Your bash Experience
Here are some common ones Windows users like to create:alias short_name='some_other_command'
alias dir='ls -l' alias del='rm' alias cp='cp --preserve=timestamps'
Example using the ls (list files) command:
Default ls command:
mmead@olga:~/digipen/cs180> ls BatchFiles.html ding.wav quiz_material Hotel California.mp3 dumpit.exe quiz_material.zip OperatingSystemConcepts8e.zip exams sandbox bash.odt foo.mmg spanish.txt batch graph-0.jpg start.wav batch.odt graph-1.jpg temp.html book makehelp.bat didcgaf-setup.txt new.css
ls using long listing (lowercase 'L': -l), and listing directories before files (--group-directories-first):
mmead@olga:~/digipen/cs180> ls -l --group-directories-first total 145,592,320 drwxr-xr-x 3 mmead mmead 4,096 2011-05-16 08:36 batch drwxr-xr-x 12 mmead mmead 4,096 2011-05-09 20:52 book drwxr-xr-x 2 mmead mmead 4,096 2011-05-18 19:29 exams drwxr-xr-x 2 mmead mmead 4,096 2011-05-09 21:11 quiz_material drwxr-xr-x 2 mmead mmead 4,096 2011-05-12 11:45 sandbox -rw-r--r-- 1 mmead mmead 14,188 2011-05-18 19:25 bash.odt -rw-r--r-- 1 mmead mmead 56,690 2011-05-15 20:39 BatchFiles.html -rw-r--r-- 1 mmead mmead 30,568 2011-05-15 11:52 batch.odt -rw------- 1 mmead mmead 3,480 2010-10-10 22:09 didcgaf-setup.txt -rwxr-xr-x 1 mmead mmead 80,856 2001-08-23 05:00 ding.wav -rw-r--r-- 1 mmead mmead 58,880 2011-05-10 20:19 dumpit.exe -rw-r--r-- 1 mmead mmead 0 2011-05-18 19:20 foo.mmg -rw-r--r-- 1 mmead mmead 25,932 2010-03-30 11:49 graph-0.jpg -rw-r--r-- 1 mmead mmead 25,679 2010-03-30 11:52 graph-1.jpg -rw------- 1 mmead mmead 6,533,037 2010-10-31 17:22 Hotel California.mp3 -rw-r--r-- 1 mmead mmead 4,291 2011-05-16 08:34 makehelp.bat -rw------- 1 mmead mmead 11,140 2011-04-20 12:58 new.css -rw-r--r-- 1 mmead mmead 138,465,681 2011-05-09 20:53 OperatingSystemConcepts8e.zip -rw-r--r-- 1 mmead mmead 52,876 2011-05-06 18:30 quiz_material.zip -rw-r--r-- 1 mmead mmead 153 2010-10-16 10:57 spanish.txt -rwxr-xr-x 1 mmead mmead 1,192 2001-08-23 05:00 start.wav -rw-r--r-- 1 mmead mmead 1,253 2011-05-18 19:33 temp.html
Same as above but enabling color output (--color):
Creating an alias for the above command in your .bashrc file:
mmead@olga:~/digipen/cs180> ls -l --group-directories-first --color=auto total 145,592,320 drwxr-xr-x 3 mmead mmead 4,096 2011-05-16 08:36 batch drwxr-xr-x 12 mmead mmead 4,096 2011-05-09 20:52 book drwxr-xr-x 2 mmead mmead 4,096 2011-05-18 19:29 exams drwxr-xr-x 2 mmead mmead 4,096 2011-05-09 21:11 quiz_material drwxr-xr-x 2 mmead mmead 4,096 2011-05-12 11:45 sandbox -rw-r--r-- 1 mmead mmead 14,188 2011-05-18 19:25 bash.odt -rw-r--r-- 1 mmead mmead 56,690 2011-05-15 20:39 BatchFiles.html -rw-r--r-- 1 mmead mmead 30,568 2011-05-15 11:52 batch.odt -rw------- 1 mmead mmead 3,480 2010-10-10 22:09 didcgaf-setup.txt -rwxr-xr-x 1 mmead mmead 80,856 2001-08-23 05:00 ding.wav -rw-r--r-- 1 mmead mmead 58,880 2011-05-10 20:19 dumpit.exe -rw-r--r-- 1 mmead mmead 0 2011-05-18 19:20 foo.mmg -rw-r--r-- 1 mmead mmead 25,932 2010-03-30 11:49 graph-0.jpg -rw-r--r-- 1 mmead mmead 25,679 2010-03-30 11:52 graph-1.jpg -rw------- 1 mmead mmead 6,533,037 2010-10-31 17:22 Hotel California.mp3 -rw-r--r-- 1 mmead mmead 4,291 2011-05-16 08:34 makehelp.bat -rw------- 1 mmead mmead 11,140 2011-04-20 12:58 new.css -rw-r--r-- 1 mmead mmead 138,465,681 2011-05-09 20:53 OperatingSystemConcepts8e.zip -rw-r--r-- 1 mmead mmead 52,876 2011-05-06 18:30 quiz_material.zip -rw-r--r-- 1 mmead mmead 153 2010-10-16 10:57 spanish.txt -rwxr-xr-x 1 mmead mmead 1,192 2001-08-23 05:00 start.wav -rw-r--r-- 1 mmead mmead 1,253 2011-05-18 19:33 temp.html
Now you can simply type dir to get the above output. Oh, and to get all of your numbers to have those nice commas in them, add this line to your startup script:alias dir='ls -l --color --group-directories-first'
export BLOCK_SIZE=\'1
This is how you would do it:
export PS1="\[\033[1;33m\]\u@\h:\w>\[\033[0m\]"
The highlighted portion is what's interesting. The '1' means bold and the '33' means yellow. Now, the prompt will be bold yellow on
black (the default background color). You could add a third number, say, '44' (1;33;44)which means a blue background.
The prompt is now hard-coded to be yellow.
However, I want the prompt to change based on
what role I'm playing (normal user or root). I want yellow for a normal user and red for root. I'm going to write a function that
will determine that and set the correct color based on that.
Then, change the prompt to call the function:# Assume that my user ID is 1234 # 33=yellow, 31=red, admin/root/superuser is 0 user_color() { if [ "$UID" -eq "0" ]; then echo 31 elif [ "$UID" -eq "1234" ]; then echo 33 fi }
export PS1="\[\033[\$(user_color)m\]\u@\h:\w>\[\033[0m\]"
Consult this page to learn about
all of the characters used above.
dircolors Abbreviations fi = normal file di = directory ln = symbolic link pi = fifo pipe so = socket bd = block device cd = character device or = symbolic link to nonexistent file (orphan) mi = symbolic link pointing to non-existent file ex = executable
Color Codes Color Codes (cont.) 00 = normal 01 = bold 04 = underlined 05 = blinking 07 = reversed 08 = concealed 31 = red 32 = green 33 = orange 34 = blue 35 = purple 36 = cyan 37 = grey 40 = black background 41 = red background 42 = green background 43 = orange background 44 = blue background 45 = purple background 46 = cyan background 47 = grey background 90 = dark grey 91 = light red 92 = light green 93 = yellow 94 = light blue 95 = light purple 96 = turquoise 100 = dark grey background 101 = light red background 102 = light green background 103 = yellow background 104 = light blue background 105 = light purple background 106 = turquoise backgroundSee the man pages for dircolors and dir_colors
The power of grep:# Add color to grep export GREP_COLOR="44;37;01" export GREP_OPTIONS="--color=auto"
$ grep -i "^a.*b.*c.*d.*" /usr/share/dict/american-english abdicated abducted abracadabra abracadabra's abscessed abscond absconded absconding absconds abstracted abstractedly $ grep -i "^b.*s.*g.*d.*" /usr/share/dict/american-english besieged
# Add color to man pages export LESS_TERMCAP_mb=$'\E[01;31m' # begin blinking export LESS_TERMCAP_md=$'\E[01;34m' # begin bold export LESS_TERMCAP_me=$'\E[0m' # end mode export LESS_TERMCAP_se=$'\E[0m' # end standout-mode export LESS_TERMCAP_so=$'\E[01;44;33m' # begin standout-mode - info box export LESS_TERMCAP_ue=$'\E[0m' # end underline export LESS_TERMCAP_us=$'\E[01;32m' # begin underline
The bash shell has all of the modern shell features you would want.
You'll see something like this:whereis chmod
However, if you just type whe and then press TAB (<TAB> is the Tab key)chmod: /bin/chmod /usr/share/man/man1/chmod.1.gz /usr/share/man/man2/chmod.2.gz
it will complete it to this:whe<TAB>
This is especially useful if the filenames or commands are very long. Not only does it save lots of typing, but you can't make a typo! (Read It! Learn It! Live it!)whereis
If there is more than one command or file that will complete what you've typed, bash won't complete it because it's ambiguous. In this case, pressing the TAB key twice quickly, will show you a list of possible candidates. For example, typing this
will show something like this (depending on what packages you have installed):wh<TAB><TAB>
You must provide additional characters so the completion is no longer ambiguous.whatis whereis which while whiptail who whoami whois
The alternative is to have the TAB key cycle through the choices. So, in the example above, typing TAB will first show whatis and pressing TAB a second time will show whereis. Pressing TAB will cycle through the choices. This is especially useful if the first several characters in a filename match (which is common). Suppose you had these two filenames in a directory:
With the default method, you would have to type: filename-a<TAB> or filename-b<TAB> to complete it, which is useless. With the cycling method, pressing TAB once gives filename-a.txt and pressing it a second time gives filename-b.txt. Perfect!filename-a.txt filename-b.txt
To enable this cycling method, add this to your .bashrc file:
bind '"\t":menu-complete'
In this case, you want to search for that command. Let's say you ran this command 20 minutes ago:
You wouldn't want to type that again, and it may be very far back in your command history. To search, type Ctrl-r (Control and lowercase R). You'll see this:valgrind -q --leak-check=full --show-reachable=yes --tool=memcheck ./memmgr 1 2
Now, as you type characters, whatever previous command matches will be shown. If the only command you've typed since that long command starts with the lowercase letter 'V', then it will complete it as soon as you press 'v'. The best way to see how this works is to try it. Warning, once you've used it, you will not be able to live without it! Also, depending on your history settings, you might be able to not only call up commands you typed 20 minutes, but commands you typed 20 weeks ago or 20 months ago or longer!(reverse-i-search)`':
But there's more! You don't have to type the first characters in the command. If you've used the valgrind command with dozens of different options, you'd have to cycle through all of them to find the one you want. If you pick something a little unique within the command line, it will jump to that one. For example, typing Ctrl-r and then the characters t o o will match the --tool=memcheck portion and show the entire command with that substring it it. Very, very cool!
it will not complete the command. If it does, it's not completing it with what I want, which is this:ls d<TAB>
If you want the completion to ignore the case of the characters (case-insensitive) then you have to configure your environment for that. However, the configuration doesn't go into your .bashrc, it goes into your .inputrc file. It's likely that you don't have a .inputrc in your home directory, so you'll have to create one with any text editor. In your .inputrc file, simply place this one line at the top:ls Downloads
Now, when you open up a new bash shell, the completions will be case-insensitive.set completion-ignore-case on
If the file exists, just open it with a text editor and add the lines at the bottom. If the file doesn't exist, it will be created. gedit is a decent text editor for new Linux users:
Add these lines to the file:gedit ~/.bashrc
You can add as much stuff as you want to the file.# Now, the dir command will display files properly :) alias dir='ls -l --color --group-directories-first' # Add color to grep output export GREP_COLOR="44;37;01" export GREP_OPTIONS="--color=auto" # Add color to man pages export LESS_TERMCAP_mb=$'\E[01;31m' # begin blinking export LESS_TERMCAP_md=$'\E[01;34m' # begin bold export LESS_TERMCAP_me=$'\E[0m' # end mode export LESS_TERMCAP_se=$'\E[0m' # end standout-mode export LESS_TERMCAP_so=$'\E[01;44;33m' # begin standout-mode - info box export LESS_TERMCAP_ue=$'\E[0m' # end underline export LESS_TERMCAP_us=$'\E[01;32m' # begin underline # This causes the TAB-completion to cycle through possible matches bind '"\t":menu-complete' # This causes commas to be inserted in numbers, e.g. 1234567890 becomes 1,234,567,890 export BLOCK_SIZE=\'1
Note: Simply modifying the .bashrc in your home directory will not cause the changes to take effect. If you want them to take effect immediately, you have to type this while in your home directory:
source .bashrc
Now the changes are in effect only for the current shell. If you have other shells already open, you will have to type that command in each one. However, any new shells that you open up will have the changes.
Typing:lpath() { perl -e 'print "\n"; print join("\n", split(/:/, $ENV{'PATH'})); print "\n";' }
at the command prompt will display all of the paths in the PATH environment variable, each on its own line. Of course, there's a bazillion ways to do the same thing, and this is almost as short as it gets:lpath
It uses the tr filter (translate) to replace each colon with a newline. Nice.echo $PATH | tr : '\n'
This example is more verbose. It will extract (unzip, untar, uncompress, etc.) a file based on the file's extension.
#------------------------------------------ #------Extraction of compressed files------ # from ARCH Wiki extract() { if [ -f $1 ]; then case $1 in *.tar.bz2) tar xvjf $1 ;; *.tar.gz) tar xvzf $1 ;; *.bz2) bunzip2 $1 ;; *.rar) rar x $1 ;; *.gz) gunzip $1 ;; *.tar) tar xvf $1 ;; *.tbz2) tar xvjf $1 ;; *.tgz) tar xvzf $1 ;; *.zip) unzip $1 ;; *.Z) uncompress $1 ;; *.7z) 7z x $1 ;; *) echo "don't know how to extract '$1'..." ;; esac else echo "'$1' is not a valid file!" fi }