|
Softpanorama
(slightly skeptical)
Open Source Software Educational Society |
May the
source be with you,
but remember the KISS principle ;-)
|
Bash Tips and Tricks
See also Unix
Shell Tips and Tricks
The introduction below was adapted from article "Unix
Scripting: some Traps, Pitfalls and Recommendations" by
Marc Dobson
BASH has some very unintuitive behavior if you source a script and do not provide
a path to it: by default, when the command "source filename" does not
contain a slash (i.e. does not include a path for the file), BASH searches
the $PATH environment variable for the filename, and only if it does
not find one there it searches the current directory!!!! Furthermore this happens
whether the file has the executable bit set or not . This is counter intuitive to
say the least and as an example TCSH does NOT do this search. One can disable
this behavior with the BASH "shopt" builtin command:
Recommendation 1: when sourcing a script always use a path name
for the file or check that the correct script is the first with that name in the
path. If the script is in the current directory then use the command "source
./filename".
Recommendation 2: as a precaution you can also put the option
to change the default BASH behavior in your ".bash_profile" setup file.
Note however that this will not help if you source a file from within an executed
script:
shopt -u sourcepath
Recommendation 3: always choose a unique script name.
Unique script names can easily be obtained by prefixing the name with the project
name to the script name (e.g. onlinesw_setup for the setup script
of the Online Software project). Bad names are for example, setup,
configure, etc...
Sourcing versus Executing
The same applies to the sourced file or the standalone script. In the sourced
file, an EXIT command, will exit the shell where the SOURCE command was executed,
as it is that shell which reads each command in turn and executes it. In contrast
in a standalone script (if executed, not sourced) the EXIT command will exit only
out of the shell/interpreter which was started to execute the commands in the script.
Therefore the executed script file will just stop and return to the shell which
called it.
The danger is therefore if a standalone script is sourced instead of being executed.
If in that script file there is an EXIT command then the shell which called the
script will be exited and not just the script.
As an example take the following two scripts. Script 1 is:
#!/bin/bash
echo "Executing script2"
./script2
if [ $? -eq 0 ]; then
echo "Executing ls in /tmp/md"
ls -l /tmp/md
else
echo "Exiting"
exit 1
fi
And script 2 is:
#!/bin/bash
echo "In script 2"
if [ -e "/tmp/md" ]; then
echo "/tmp/md exists"
else
echo "/tmp/md does not exist"
exit 1
fi
Both scripts should have the execute bit set. Start a BASH shell by typing bash,
and at the next prompt execute script 1. The following output is produced:
If directory /tmp/md
exists: |
If directory /tmp/md
does not exist: |
Executing script2
In script 2
/tmp/md exists
Executing ls in /tmp/md
total 0
|
Executing script2
In script 2
/tmp/md does not exist
Exiting
|
Now change script 1 to source script 2 instead of executing it (source
./script2 instead of ./script2). When the script 1 is executed
the following output will be produced:
If directory /tmp/md
exists: |
If directory /tmp/md
does not exist: |
Executing script2
In script 2
/tmp/md exists
Executing ls in /tmp/md
total 0
|
Executing script2
In script 2
/tmp/md does not exist
|
If the directory /tmp/md exists then the output is the same and
exactly the same commands were executed. If however the directory /tmp/md
does not exist then the script 2 has an EXIT and as it was sourced from script 1,
it is actually script 1 which exits without the desired effect, i.e. printing
"Exiting". In this case it is not very important but it could have very profound
consequences with complex scripts.
The ambiguity in this case is compounded by the difference in coding in the two
branches of the IF statement of script 2. For the case when the directory exists
the EXIT command is implicit (the script goes to the end and exits normally), whereas
for the case when the directory does not exist the EXIT command is explicit (this
is the one which causes the exit from script 1).
If the programmer wishes to exit from a sourced script file (as he would with
the EXIT command in an executed script), he may do so with:
where "[n]" is the return value that can be tested for in the script/shell which
sourced the script file (as with the EXIT command). Beware though that the RETURN
command is also used to exit a function, therefore make sure that the RETURN command
is placed in the appropriate place for the desired effect.
Recommendation 4: If the script is meant to be sourced then
do not put a line at the top of the file of the format #!/bin/bash
and do not set the execute bit on this file (see the 'Warning' in the previous section
if you do not have control over the execute bit).
Recommendation 5: The EXIT command should NEVER be used in a
sourced file (sourced script) unless the developer is ABSOLUTELY sure that it does
what he intends it to do.
Recommendation 6: some people keep the top line #!/bin/bash
to show "humans" which script interpreter the commands in the file are meant for.
I would recommend that if a reminder is needed, a naming convention be used such
as: sourced file.bash, or sourced file.csh.
Recommendation 7: If the script is meant to be executed, the
first line should be of the form #!/bin/bash and the execute bit should
be set for this file.
Recommendation 8: The EXIT command can be used in an standalone
scripts without any risk with respect to other scripts which might call it.
Recommendation 9: The RETURN command can be used to exit a sourced
script file, however be carefull as it is also used to exit functions (either in
sourced or executed script files).
The above six recommendations will make it easier for people who do not know
the scripts to understand how they should be used, i.e. sourced or executed,
without any ambiguity and without having to read and understand the scripts.
If the same functionality is required (i.e. the same commands) to be sourced
in one script and executed in another you can use includes.
Put the commands in a sourced file (as opposed to a standalone script) and source
this file from the script which needs to source the commands. Create another file
which is this time a standalone script and inside this source the sourced file.
Now from the script that needs to execute the commands call the second file that
was created.
As an example, find bellow the two files to create. The first file (sourced file)
could be:
echo "The commands to run start now"
cd /tmp
ls
cd -
echo "The commands to run finish now"
|
The second file (the standalone script) could be:
#!/bin/bash
echo "We have been executed"
echo "Running the commands ..."
source ./sourced file
echo "Exiting"
|
If the commands need to be sourced then call:
and if they need to be executed then use:
2007/06/25 |
Guest Contributor, TechRepublic
1. Easily recall previous commands
Bash keeps track of the commands you execute in a history
buffer, and allows you to recall previous commands by cycling
through them with the Up and Down cursor keys. For even faster
recall, "speed search" previously-executed commands by typing
the first few letters of the command followed by the key
combination Ctrl-R; bash will then scan the command history for
matching commands and display them on the console. Type Ctrl-R
repeatedly to cycle through the entire list of matching
commands.
... ... ...
5. Get automatic notification of new mail
You can configure bash to automatically notify you of new
mail, by setting the $MAILPATH variable to point to your local
mail spool. For example, the command:
bash> MAILPATH='/var/spool/mail/john'
bash> export MAILPATH
Causes bash to print a notification on john's console every
time a new message is appended to John's mail spool.
6. Run tasks in the background
Bash lets you run one or more tasks in the background, and
selectively suspend or resume any of the current tasks (or
"jobs"). To run a task in the background, add an ampersand (&)
to the end of its command line. Here's an example:
bash> tail -f /var/log/messages &
[1] 614
Each task backgrounded in this manner is assigned a job ID,
which is printed to the console. A task can be brought back to
the foreground with the command fg jobnumber, where jobnumber is the job ID of the task you wish to bring to
the foreground. Here's an example:
bash> fg 1
A list of active jobs can be obtained at any time by typing
jobs at the bash prompt.
7. Quickly jump to frequently-used directories
You probably already know that the $PATH variable lists
bash's "search path" -- the directories it will search when it
can't find the requested file in the current directory. However,
bash also supports the $CDPATH variable, which lists the
directories the cd command will look in when attempting to
change directories. To use this feature, assign a directory list
to the $CDPATH variable, as shown in the example below:
bash> CDPATH='.:~:/usr/local/apache/htdocs:/disk1/backups'
bash> export CDPATH
Now, whenever you use the cd command, bash will check all the
directories in the $CDPATH list for matches to the directory
name.
8. Perform calculations
Bash can perform simple arithmetic operations at the command
prompt. To use this feature, simply type in the arithmetic
expression you wish to evaluate at the prompt within double
parentheses, as illustrated below. Bash will attempt to perform
the calculation and return the answer.
bash> echo $((16/2))
8
... ... ...
10. Get context-specific help
Bash comes with help for all built-in commands. To see a list
of all built-in commands, type help. To obtain help on a
specific command, type help command, where command
is the command you need help on. Here's an example:
bash> help alias
...some help text...
Obviously, you can obtain detailed help on the bash shell by
typing man bash at your command prompt at any time.
# Bash tips and tricks for History related
preferences
# see
http://richbradshaw.wordpress.com/2007/11/25/bash-tips-and-tricks/# == 1 Lost bash history ==
# the bash history is only saved when you close the
terminal, not after each command. fix it..
shopt -s histappend
PROMPT_COMMAND=’history -a’
# == 2. Stupid spelling mistakes ==
# This will make sure that spelling mistakes such as ect
instead of etc are ignored.
shopt -s cdspell
# == 3. Duplicate entries in bash history ==
# This will ignore duplicates, as well as ls, bg, fg and
exit as well, making for a cleaner bash history.
export HISTIGNORE=”&:ls:[bf]g:exit”
# == 4 Multiple line commands split up in history ==
# this will change multiple line commands into single
lines for easy editing.
shopt -s cmdhist
One thing you can do is redirect your output to a file. Basic output redirection
should be nothing new to anyone who has spent a reasonable amount of time using
any UNIX or Linux shell, so I won't go into detail regarding the basics of output
redirection. To save the useful output from the find command, you can redirect
the output to a file:
$ find / -name foo > output.txt
You still see the error messages on the screen but not the path of the file
you're looking for. Instead, that is placed in the file output.txt. When the
find command completes, you can cat the file output.txt to get the
location(s) of the file(s) you want.
That's an acceptable solution, but there's a better way. Instead of redirecting
the standard output to a file, you can redirect the error messages to a file.
This can be done by placing a 2 directly in front of the redirection angle bracket.
If you are not interested in the error messages, you simply can send them to
/dev/null:
This shows you the location of file foo, if it exists, without those pesky
permission denied error messages. I almost always invoke the find command
in this way.
The number 2 represents the standard error output stream. Standard error
is where most commands send their error messages. Normal (non-error) output
is sent to standard output, which can be represented by the number 1. Because
most redirected output is the standard output, output redirection works only
on the standard output stream by default. This makes the following two commands
equivalent:
$ find / -name foo > output.txt
$ find / -name foo 1> output.txt
Sometimes you might want to save both the error messages and the standard
output to file. This often is done with cron jobs, when you want to save all
the output to a log file. This also can be done by directing both output streams
to the same file:
$ find / -name foo > output.txt 2> output.txt
This works, but again, there's a better way to do it. You can tie the standard
error stream to the standard output stream using an ampersand. Once you do this,
the error messages goes to wherever you redirect the standard output:
$ find / -name foo > output.txt 2>&1
One caveat about doing this is that the tying operation goes at the end of
the command generating the output. This is important if piping the output to
another command. This line works as expected:
find -name test.sh 2>&1 | tee /tmp/output2.txt
but this line doesn't:
find -name test.sh | tee /tmp/output2.txt 2>&1
and neither does this one:
find -name test.sh 2>&1 > /tmp/output.txt
I started this discussion on output redirection using the find command
as an example, and all the examples used the find command. This discussion
isn't limited to the output of find, however. Many other commands can generate
enough error messages to obscure the one or two lines of output you need.
Output redirection isn't limited to bash, either. All UNIX/Linux shells
support output redirection using the same syntax.
Bash bang commands can be used for shortcuts too.
- !! = last line in history
- !* = all args from last line in history
- !$ = last arg from last line in history
- !^ = first arg from last line in history
I really only use !$ with the cd command. Here’s some examples, although
some not really useful. Just to give you an idea of what it does:
- which php (maybe it outputs /usr/local/bin/php)
- `!!` /path/to/php_script.php (executes php on the script)
Something you may have seen before in other systems (the much maligned SCO
OSes, for example) is this handy option:
shopt -s cdspell
"This will correct minor spelling errors in a 'cd'
command, so that instances of transposed characters, missing characters and
extra characters are corrected without the need for retyping."
No more worrying about casesThe best bash tip I can share is very
helpful when working on systems that don't allow filenames to differ only in
case (like OSX and Windows):
create a file called .inputrc in your home directory and put this line in
it:
set completion-ignore-case on
Now bash tab-completion won't worry about case in filenames. Thus 'cd sit[tab]'
would complete to 'cd Sites/'
Last argument
You can also use Esc-period and get the last parm of the previous line. You
can repeatedly use Esc-period to scroll back through time with them. That turns
out to be even better than $! because you can edit it once it shows up on your
command line.
should be !$
Instead of $!, use !$, it works much better. :)
$ echo asdf
asdf
$ echo !$
echo asdf
asdf
$ echo $!
$
So $! is an empty variable, while !$ brings back the last argument from the
last command.
Command substitution
$ for s in `cat server.list`; do ssh $s uptime; done;
Command substution is also done using $(command)
notation, which I prefer to the backquotes. It allows commands to be nested
(backquotes allow that too, but the inner quotes must be escaped using backslashes,
which gets messy.
For example:
$ for s in $(cat server.list); do echo "$s: $(ssh $s uptime)"; done;
or:
# get the uptime for just the first server
$ echo "$(date): $(ssh $(head -1 server.list) uptime)"
=====
More key bindings and tricks
Bash will keep a history of the directories you visit, you just have to ask.
You can also always go back to the previous directory you were in by typing
cd - without the need to pushd the current directory. Using
it more than once cycles between the current and previous directory.
CTRL-A takes you to the beginning of the line and CTRL-E takes you to
the end of the line. This is probably basic shell knowledge,
I think it's actually common readline/emacs knowledge, and it works in much
more programs than just Bash or a terminal. For instance, you can enable them
in Gnome applications by adding the line
gtk-key-theme-name = "Emacs" to the ~/.gtkrc-2.0 file.
Other handy key bindings you can use are:
- ctrl-u : Cut everything on the current line before the cursor.
- ctrl-y : 'Yank' (paste) text that was cut using ctrl-u.
- ctrl-w : Delete the word on the left of the cursor
There's so much usefull knowledge hidden in Bash that, if you spend any time
at the command line, you should really get yourself aquinted with. It saves
incredible ammounts of time.
Take for example something I wanted to do yesterday. I wanted to now the
number of hits on a certain website. I could have installed a tool to parse
the Apache access.log, but this was much easier:
$ cat access.log | cut -d"[" -f2 | cut -d"]" -f1 | cut -d"/" -f2 | uniq
-c
28905 Mar
16554 Apr
Takes no more than a couple of seconds to write, but saves so much time.
Try reading through the Bash man page. It's huge, but think of all the stuff
you'll learn! Or read some online Bash scripting tutorials. Everything from
gathering statistics from files to creating thumbnails of images (From the top
of my head: for A in *; do convert $A -resize 140x140 th_$A; done)
becomes a cinch.
Flip the Last Two Characters
If you type like me your fingers spit characters out in the wrong order on
occasion. ctrl-t swaps the order that the last two character appear in.
Searching Bash History
As you enter commands at the CLI they are saved in a file ~./.bash_history.
From the bash prompt you can browse the most recently used commands through
the least recently used commands by pressing the up arrow. Pressing the down
arrow does the opposite.
If you have entered a command a long time ago and need to execute it again
you can search for it. Type the command 'ctrl-r' and enter the text you want
to search for.
Good sample bashrc file
This Is Your Open Enterprise™
Skip to Content United States - EnglishNovell Home LoginDownload
Products & Solutions
Services & Support
Partners & Communities
Search
Advanced Search
SolutionsIdentity, Security and Systems ManagementLinux Operating SystemsWorkgroup CollaborationProducts forIndustriesSmall BusinessProducts A-ZServicesConsultingTrainingSupportCustomer CenterDiscussion ForumsDocumentationKnowledgebasePatches & SecuritySupport by ProductPartnersCertified Partner ProductsFind a PartnerPartner with NovellStrategic PartnersCommunitiesBlogsConnection MagazineCool SolutionsDevelopersNovell Users Intl. (NUI)Partner
> cool solutions home > cool tools home
Bash - Making use of your .bashrc file
Novell Cool Solutions: Cool ToolRate This Page
Reader Rating from 4 ratings
Printer Friendlytell a friendDigg This - Slashdot This
In Brief
A sample .bashrc file.
Vitals
Product Categories:
Open Enterprise Server
SUSE Linux
SUSE Linux Enterprise Desktop
SUSE Linux Enterprise Server
Functional Categories:
BASH
Shortcuts
Workgroup
Updated: 23 Oct 2006
File Size: 6.9KB
License: GPL
Download: /coolsolutions/tools/downloads/bashrc.txt
Publisher: David Crouse
Disclaimer
Please read the note from our friends in legal before using this file.
Details
I was playing with my .bashrc file again, and was once again impressed by how you can tweak Linux to do what YOU want it to do so easily. I am sure there are tons of other tweaks you can do to your .bashrc file, but I really like some of mine, and thought I would share them. Some of the alias's I created, some I found on the net, and some things in my .bashrc file are just there for fun, like the "# WELCOME SCREEN", although it does serve a purpose for me at the same time, it might not be something everyone would want or need.
For those that don't know what a .bashrc file does: "The ~/.bashrc file determines the behavior of interactive shells." Quoted From: The Advanced Bash Scripting Guide
Basically , it allows you to create shortcuts (alias's) and interactive programs (functions) that run on the startup of the bash shell or that are used when running an interactive shell. For example, it's much easier to just type: ebrc instead of pico ~/.bashrc (I used the alias ebrc , and it stands for "Edit Bash RC file". I could have also aliased it to just use one letter, making it a VERY fast short cut. The bashrc file allows you to create alias's (shortcuts) to almost anything you want. My list is pretty long, but I'm sure there is someone with a longer list ;)
I have my .bashrc file setup in sections. The following is the breakdown by section of how I keep my list of alias's and functions separated. This is just how I do this, your .bashrc file can be modified to suit YOUR needs, that's the interesting part about the .bashrc file. It's VERY customizable and very easy to change.
Header (So I know when i modified it last and what i was running it on)
Exports (So I can set history size, paths , editors, define colors, etc,)
Sourced Alias's (So I can find those hidden alias's faster)
Workstation Alias's (so i can ssh to local machines quickly)
Remote Server Alias's (so i can ssh to remote servers easily)
Script Alias's (quick links to some of my bashscripts)
Hardware control alias's (so I can control cd/dvd/scanners/audio/etc)
Modified commands (Alias's to normal linux commands with special flags)
Chmod Alias's (makes changing permissions faster)
Alias's for GUI programs (start firefox, etc from command line)
Alias's for xterm and others (open xterm with special settings)
Alias's for Lynx (open lynx with urls - kind of a bash bookmark ;) )
UNused Alias's (Alias's that aren't in use on the system, but that i might use later)
Special functions (more of a function than just an alias..it goes here)
Notes (that should be self explanatory ;) )
Welcome Screen (code to make my bash shell display some stuff as it starts up)
That's how I lay out my .bashrc files. It may not be perfect, but it works well for me. I like making changes in just my .bashrc file and not the global files. I like the .bashrc file because you don't need root permissions to make changes that make your life easier at the bash shell.
The following is my .bashrc file (with some things obviously commented out for security... but most of it should be self explanatory). Anyone with comments/suggestions/ideas feel free to let me know. I'm always looking for new and interesting things to do with the .bashrc file.
Want to know what alias's your bash shell has? Simply type the word alias at the command line. The shell will then print out the list of active alias's to the standard output (normally your screen).
#######################################################
# Dave Crouse's .bashrc file
# www.bashscripts.org
# www.usalug.org
#
# Last Modified 04-08-2006
# Running on OpenSUSE 10
#######################################################
# EXPORTS
#######################################################
PATH=$PATH:/usr/lib/festival/ ;export PATH
export PS1="[\[\033[1;34m\w\[\033[0m]\n[\t \u]$ "
export EDITOR=/usr/bin/pico
export HISTFILESIZE=3000 # the bash history should save 3000 commands
export HISTCONTROL=ignoredups #don't put duplicate lines in the history.
alias hist='history | grep $1' #Requires one input
# Define a few Color's
BLACK='\e[0;30m'
BLUE='\e[0;34m'
GREEN='\e[0;32m'
CYAN='\e[0;36m'
RED='\e[0;31m'
PURPLE='\e[0;35m'
BROWN='\e[0;33m'
LIGHTGRAY='\e[0;37m'
DARKGRAY='\e[1;30m'
LIGHTBLUE='\e[1;34m'
LIGHTGREEN='\e[1;32m'
LIGHTCYAN='\e[1;36m'
LIGHTRED='\e[1;31m'
LIGHTPURPLE='\e[1;35m'
YELLOW='\e[1;33m'
WHITE='\e[1;37m'
NC='\e[0m' # No Color
# Sample Command using color: echo -e "${CYAN}This is BASH
${RED}${BASH_VERSION%.*}${CYAN} - DISPLAY on ${RED}$DISPLAY${NC}\n"
# SOURCED ALIAS'S AND SCRIPTS
#######################################################
### Begin insertion of bbips alias's ###
source ~/.bbips/commandline/bbipsbashrc
### END bbips alias's ###
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# enable programmable completion features
if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
# ALIAS'S OF ALL TYPES SHAPES AND FORMS ;)
#######################################################
# Alias's to local workstations
alias tom='ssh 192.168.2.102 -l root'
alias jason='ssh 192.168.2.103 -l root'
alias randy='ssh 192.168.2.104 -l root'
alias bob='ssh 192.168.2.105 -l root'
alias don='ssh 192.168.2.106 -l root'
alias counter='ssh 192.168.2.107 -l root'
# ALIAS TO REMOTE SERVERS
alias ANYNAMEHERE='ssh YOURWEBSITE.com -l USERNAME -p PORTNUMBERHERE'
# My server info removed from above for obvious reasons ;)
# Alias's to TN5250 programs. AS400 access commands.
alias d1='xt5250 env.TERM = IBM-3477-FC env.DEVNAME=D1 192.168.2.5 &'
alias d2='xt5250 env.TERM = IBM-3477-FC env.DEVNAME=D2 192.168.2.5 &'
alias tn5250j='nohup java -jar /home/crouse/tn5250j/lib/tn5250j.jar
2>>error.log &'
# Alias's to some of my BashScripts
alias bics='sh /home/crouse/scripts/bics/bics.sh'
alias backup='sh /home/crouse/scripts/usalugbackup.sh'
alias calc='sh /home/crouse/scripts/bashcalc.sh'
alias makepdf='sh /home/crouse/scripts/makepdf.sh'
alias phonebook='sh /home/crouse/scripts/PHONEBOOK/baps.sh'
alias pb='sh /home/crouse/scripts/PHONEBOOK/baps.sh'
alias ppe='/home/crouse/scripts/passphraseencryption.sh'
alias scripts='cd /home/crouse/scripts'
# Alias's to control hardware
alias cdo='eject /dev/cdrecorder'
alias cdc='eject -t /dev/cdrecorder'
alias dvdo='eject /dev/dvd'
alias dvdc='eject -t /dev/dvd'
alias scan='scanimage -L'
alias playw='for i in *.wav; do play $i; done'
alias playo='for i in *.ogg; do play $i; done'
alias playm='for i in *.mp3; do play $i; done'
alias copydisk='dd if=/dev/dvd of=/dev/cdrecorder' # Copies bit by bit
from dvd to cdrecorder drives.
alias dvdrip='vobcopy -i /dev/dvd/ -o ~/DVDs/ -l'
# Alias's to modified commands
alias ps='ps auxf'
alias home='cd ~'
alias pg='ps aux | grep' #requires an argument
alias un='tar -zxvf'
alias mountedinfo='df -hT'
alias ping='ping -c 10'
alias openports='netstat -nape --inet'
alias ns='netstat -alnp --protocol=inet | grep -v CLOSE_WAIT | cut
-c-6,21-94 | tail +2'
alias du1='du -h --max-depth=1'
alias da='date "+%Y-%m-%d %A %T %Z"'
alias ebrc='pico ~/.bashrc'
# Alias to multiple ls commands
alias la='ls -Al' # show hidden files
alias ls='ls -aF --color=always' # add colors and file type extensions
alias lx='ls -lXB' # sort by extension
alias lk='ls -lSr' # sort by size
alias lc='ls -lcr' # sort by change time
alias lu='ls -lur' # sort by access time
alias lr='ls -lR' # recursive ls
alias lt='ls -ltr' # sort by date
alias lm='ls -al |more' # pipe through 'more'
# Alias chmod commands
alias mx='chmod a+x'
alias 000='chmod 000'
alias 644='chmod 644'
alias 755='chmod 755'
# Alias Shortcuts to graphical programs.
alias kwrite='kwrite 2>/dev/null &'
alias firefox='firefox 2>/dev/null &'
alias gaim='gaim 2>/dev/null &'
alias kate='kate 2>/dev/null &'
alias suk='kdesu konqueror 2>/dev/null &'
# Alias xterm and aterm
alias term='xterm -bg AntiqueWhite -fg Black &'
alias termb='xterm -bg AntiqueWhite -fg NavyBlue &'
alias termg='xterm -bg AntiqueWhite -fg OliveDrab &'
alias termr='xterm -bg AntiqueWhite -fg DarkRed &'
alias aterm='aterm -ls -fg gray -bg black'
alias xtop='xterm -fn 6x13 -bg LightSlateGray -fg black -e top &'
alias xsu='xterm -fn 7x14 -bg DarkOrange4 -fg white -e su &'
# Alias for lynx web browser
alias bbc='lynx -term=vt100 http://news.bbc.co.uk/text_only.stm'
alias nytimes='lynx -term=vt100 http://nytimes.com'
alias dmregister='lynx -term=vt100 http://desmoinesregister.com'
# SOME OF MY UNUSED ALIAS's
#######################################################
# alias d=`echo "Good Morning Dave. today's date is" | festival --tts;
date +'%A %B %e' | festival --tts`
# alias shrink84='/home/crouse/shrink84/shrink84.sh'
# alias tl='tail -f /var/log/apache/access.log'
# alias te='tail -f /var/log/apache/error.log'
# SPECIAL FUNCTIONS
#######################################################
netinfo ()
{
echo "--------------- Network Information ---------------"
/sbin/ifconfig | awk /'inet addr/ {print $2}'
echo ""
/sbin/ifconfig | awk /'Bcast/ {print $3}'
echo ""
/sbin/ifconfig | awk /'inet addr/ {print $4}'
# /sbin/ifconfig | awk /'HWaddr/ {print $4,$5}'
echo "---------------------------------------------------"
}
spin ()
{
echo -ne "${RED}-"
echo -ne "${WHITE}\b|"
echo -ne "${BLUE}\bx"
sleep .02
echo -ne "${RED}\b+${NC}"
}
scpsend ()
{
scp -P PORTNUMBERHERE "$@"
USERNAME@YOURWEBSITE.com:/var/www/html/pathtodirectoryonremoteserver/;
}
# NOTES
#######################################################
# To temporarily bypass an alias, we preceed the command with a \
# EG: the ls command is aliased, but to use the normal ls command you would
# type \ls
# mount -o loop /home/crouse/NAMEOFISO.iso /home/crouse/ISOMOUNTDIR/
# umount /home/crouse/NAMEOFISO.iso
# Both commands done as root only.
# WELCOME SCREEN
#######################################################
clear
for i in `seq 1 15` ; do spin; done ;echo -ne "${WHITE} USA Linux Users
Group ${NC}"; for i in `seq 1 15` ; do spin; done ;echo "";
echo -e ${LIGHTBLUE}`cat /etc/SUSE-release` ;
echo -e "Kernel Information: " `uname -smr`;
echo -e ${LIGHTBLUE}`bash --version`;echo ""
echo -ne "Hello $USER today is "; date
echo -e "${WHITE}"; cal ; echo "";
echo -ne "${CYAN}";netinfo;
mountedinfo ; echo ""
echo -ne "${LIGHTBLUE}Uptime for this computer is ";uptime | awk /'up/
{print $3,$4}'
for i in `seq 1 15` ; do spin; done ;echo -ne "${WHITE} http://usalug.org
${NC}"; for i in `seq 1 15` ; do spin; done ;echo "";
echo ""; echo ""The following belong under the "function" section in my .bashrc. Useable as seperate programs, I've integrated them simply as functions for my .bashrc file in order to make them quick to use and easy to modify and find. These are functions that are used to symetrically encrypt and to decrypt files and messages. Some are completely command line, and the last two create gui interfaces to locate the files to encrypt/decrypt. If you create a program out of the functions creating a link via a shortcut/icon on the desktop would create a completely gui based interface to locate and encrypt/decrypt files. Either way, it's an easy way to use gpg.
Requires: zenity, gpg
################### Begin gpg functions ##################
encrypt ()
{
# Use ascii armor
gpg -ac --no-options "$1"
}
bencrypt ()
{
# No ascii armor
# Encrypt binary data. jpegs/gifs/vobs/etc.
gpg -c --no-options "$1"
}
decrypt ()
{
gpg --no-options "$1"
}
pe ()
{
# Passphrase encryption program
# Created by Dave Crouse 01-13-2006
# Reads input from text editor and encrypts to screen.
clear
echo " Passphrase Encryption Program";
echo "--------------------------------------------------"; echo "";
which $EDITOR &>/dev/null
if [ $? != "0" ];
then
echo "It appears that you do not have a text editor set in your
.bashrc file.";
echo "What editor would you like to use ? " ;
read EDITOR ; echo "";
fi
echo "Enter the name/comment for this message :"
read comment
$EDITOR passphraseencryption
gpg --armor --comment "$comment" --no-options --output
passphraseencryption.gpg --symmetric passphraseencryption
shred -u passphraseencryption ; clear
echo "Outputting passphrase encrypted message"; echo "" ; echo "" ;
cat passphraseencryption.gpg ; echo "" ; echo "" ;
shred -u passphraseencryption.gpg ;
read -p "Hit enter to exit" temp; clear
}
keys ()
{
# Opens up kgpg keymanager
kgpg -k
}
encryptfile ()
{
zenity --title="zcrypt: Select a file to encrypt" --file-selection > zcrypt
encryptthisfile=`cat zcrypt`;rm zcrypt
# Use ascii armor
# --no-options (for NO gui usage)
gpg -acq --yes ${encryptthisfile}
zenity --info --title "File Encrypted" --text "$encryptthisfile has been
encrypted"
}
decryptfile ()
{
zenity --title="zcrypt: Select a file to decrypt" --file-selection > zcrypt
decryptthisfile=`cat zcrypt`;rm zcrypt
# NOTE: This will OVERWRITE existing files with the same name !!!
gpg --yes -q ${decryptthisfile}
zenity --info --title "File Decrypted" --text "$encryptthisfile has been
decrypted"
}
################### End gpg functions ##################
Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions. www.webwiseone.com
Reader Comments
cool man, really cool. i love such stuffs you know. working in the command line makes you feel like a real linux geek
it's really cool. good job.
AuthorsDocumentationGlossaryKnowledgebaseNovell ConnectionPartner Product GuideSupport ForumsTrainingAppNotes by DateAppNotes by TitleSubmit an AppNoteCool Tools by ProductCool Tools by Tool NameCool Tools by DateCool Tools by CategoryCool Tools by File NameCool Tools by PublisherSubmit a ToolAdvertising in Cool SolutionsTalk to UsSubmit a TipSubmit an AppNoteSubscribeXML/RSS News FeedsFirefox FeedsJavascript News FeedsAccess ManagerAuditBorderManagereDirectoryExteNdIdentity ManagerSecureLoginSentinelZENworksSUSE Linux Enterprise DesktopSUSE Linux Enterprise ServerGroupWiseOpen Enterprise ServerSUSE Linux Enterprise DesktopTeaming + ConferencingIdentity, Security, & Systems ManagementLinux Operating SystemsWorkgroupCool Solutions dot ComCool Solutions HomeResourcesAppNotesCool ToolsGet InvolvedCool Solutions to GoOther CoolsCool BlogsCool Solutions WikiOpen Audio (podcasts)
1.800.529.3400 local numbers
Request Call
Corporate Governance | Legal & Export | Privacy | Subscribe | Feedback | Glossary | RSS | Contact | Printer Friendly
© 2007 Novell, Inc. All Rights Reserved.
xargs, find and several useful shortcuts
See also Unix Xargs and
Unix Find Command pages.
Re:pushd and popd (and other tricks) (Score:2)
by Ramses0 (63476)
on Wednesday March 10, @07:39PM (#8527252)
My favorite "Nifty" was when I spent the time to learn about "xargs" (I
pronounce it zargs), and brush up on "for" syntax.
ls | xargs -n 1 echo "ZZZ>
"
Basically indents (prefixes) everything with a "ZZZ" string. Not really useful,
right? But since it invokes the echo command (or whatever command you specify)
$n times (where $n is the number of lines passed to it) this saves me from having
to write a lot of crappy little shell scripts sometimes.
A more serious example is:
find -name \*.jsp | sed
's/^/http:\/\/127.0.0.1/server/g' | xargs -n 1 wget
...will find all your jsp's, map them to your localhost webserver, and invoke
a wget (fetch) on them. Viola, precompiled JSP's.
Another:
for f in `find -name \*.jsp`
; do echo "==> $f" >> out.txt ; grep "TODO" $f >> out.txt ; done
...this searches JSP's for "TODO" lines and appends them all to a file with
a header showing what file they came from (yeah, I know grep can do this, but
it's an example. What if grep couldn't?) ...and finally...
( echo "These were the command line params"
echo "---------"
for f in $@ ; do
echo "Param: $f"
done ) | mail -s "List" you@you.com ...the parenthesis
let your build up lists of things (like interestingly formatted text) and it
gets returned as a chunk, ready to be passed on to some other shell processing
function.
Shell scripting has saved me a lot of time in my life, which I am grateful for.
:^) |
[May 7, 2007] To strip file extensions in bash, like this.rbl
--> this
fname=${file%.rbl}
Last argument reuse
tail -f /tmp/foo
rm !$ # !$ is the last argument to the previous command.
Correction sed style
grep 'wibble' afile | lwss #typo: meant to type less
!!:s/lw/le #!! is last command string, :s does sed-style modification. :gs does a global replace
# or for simpler corrections
# n.b. textile screws this up. replace the sup elements with circumflexes.
cat .bash_profilx #typo - meant the x to be an e
<sup>x</sup>e # 'repeat last command, subsituting x for e
touch a{1,2,3,4}b # brace gets expanded to a1b a2b a3b a4b so 4 files get touched
cp file{,.old} # brace gets expanded to file file.old , thus creating a backup.
- shell variables
-
- $CDPATH
- This is a little known and very underrated shell variable.
CDPATH does for the cd built-in what PATH
does for executables. By setting this wisely, you can cut down on the
number of key-strokes you enter per day.
Try this:
$ export CDPATH=.:~:~/docs:~/src:~/src/ops/docs:/mnt:/usr/src/redhat:/usr/src/redhat/RPMS:/usr/src:/usr/lib:/usr/local:/software:/software/redhat
Using this, cd i386 would likely take you to /usr/src/redhat/RPMS/i386
on a Red Hat Linux system.
- $HISTIGNORE
- Set this to to avoid having consecutive duplicate commands and other
not so useful information appended to the history list. This will cut
down on hitting the up arrow endlessly to get to the command before
the one you just entered twenty times. It will also avoid filling a
large percentage of your history list with useless commands.
Try this:
$ export HISTIGNORE="&:ls:ls *:mutt:[bf]g:exit"
Using this, consecutive duplicate commands, invocations of
ls, executions of the
mutt mail client
without any additional parameters, plus calls to the bg,
fg and exit built-ins will not be
appended to the history list.
- $MAILPATH
- bash will warn you of new mail in any folder appended to MAILPATH.
This is very handy if you use a tool like
procmail to presort
your e-mail into folders.
Try adding the following to your ~/.bash_profile to be notified
when any new mail is deposited in any mailbox under ~/Mail.
MAILPATH=/var/spool/mail/$USER
for i in `echo ~/Mail/[^.]*`
do
MAILPATH=$MAILPATH:$i
done
export MAILPATH
unset i
If you use mutt
and many of those folders don't receive automatically filtered mail,
you may prefer to have bash alert you only when new e-mail arrives in
a folder that you also track in mutt.
In that case, try something like the following in your ~/.bash_profile:
export `perl -ne 's/^mailboxes /MAILPATH=/ && tr/ /:/ && print && exit' < ~/.muttrc`
- $TMOUT
- If you set this to a value greater than zero, bash will terminate
after this number of seconds have elapsed if no input arrives.
This setting is useful in root's environment to reduce the potential
security risk of someone forgetting to log out as the superuser.
- set options
-
- ignoreeof
- Ordinarily, issuing Ctrl-D at the prompt will log you
out of an interactive shell. This can be annoying if you regularly need
to type Ctrl-D in other situations, for example, when trying
to disconnect from a Telnet session. In such a situation, hitting
Ctrl-D once too often will close your shell, which can be
very frustrating. This option disables the use of Ctrl-D
to exit the shell.
- shopt options
-
You can set each of the options below with shopt -s <option>.
- cdspell
- This will correct minor spelling errors in a cd
command, so that instances of transposed characters, missing characters
and extra characters are corrected without the need for retyping.
- cmdhist
- This is very much a matter of taste. Defining this will cause multi-line
commands to be appended to your bash history as a single line command.
This makes for easy command editing.
- dotglob
- This one allows files beginning with a dot ('.') to be returned
in the results of path-name expansion.
- extglob
- This will give you ksh-88 egrep-style extended pattern matching
or, in other words, turbo-charged pattern matching within bash.
The available operators are:
- ?(pattern-list)
- Matches zero or one occurrence of the given patterns
- *(pattern-list)
- Matches zero or more occurrences of the given patterns
- +(pattern-list)
- Matches one or more occurrences of the given patterns
- @(pattern-list)
- Matches exactly one of the given patterns
- !(pattern-list)
- Matches anything except one of the given patterns
Here's an example. Say, you wanted to install all RPMs in a given
directory, except those built for the noarch architecture.
You might use something like this:
rpm -Uvh /usr/src/RPMS/!(*noarch*)
These expressions can be nested, too, so if you wanted a directory
listing of all non PDF and PostScript files in the current directory,
you might do this:
ls -lad !(*.p?(df|s))
readline Tips and Tricks
The readline library is used by bash and many other programs to read a line from
the terminal, allowing the user to edit the line with standard
Emacs editing keys.
- set show-all-if-ambiguous on
If you have this in your /etc/inputrc or ~/.inputrc, you
will no longer have to hit the <Tab> key twice to produce a list
of all possible completions. A single <Tab> will suffice. This setting
is highly recommended.
- set visible-stats on
Adding this to your /etc/inputrc or ~/.inputrc will result
in a character being appended to any file-names returned by completion, in much
the same way as ls -F works.
- If you're a fan of vi as opposed to Emacs, you might prefer to operate bash
in vi editing mode. Being a GNU program, bash uses Emacs bindings unless you
specify otherwise.
Set the following in your /etc/inputrc or ~/.inputrc:
set editing-mode vi
set keymap vi
and this in your /etc/bashrc or ~/.bashrc:
set -o vi
Set Vi Mode in Bash
Vi mode allows for the use of vi like commands when at the bash prompt. When
set to this mode initially you will be in insert mode (be able to type at the
prompt unlike when you enter vi). Hitting the escape key takes you into
command mode.
Useful Commands and Features
The commands in this section are non-mode specific, unlike the ones listed above.
Flip the Last Two Characters
If you type like me your fingers spit characters out in the wrong order on
occasion. ctrl-t swaps the order that the last two character appear in.
Searching Bash History
As you enter commands at the CLI they are saved in a file ~./.bash_history.
From the bash prompt you can browse the most recently used commands through
the least recently used commands by pressing the up arrow. Pressing the down
arrow does the opposite.
If you have entered a command a long time ago and need to execute it again
you can search for it. Type the command 'ctrl-r' and enter the text you want
to search for.
Dealing with Spaces
First, I will mention a few ways to deal with spaces in directory names,
file names, and everywhere else.
Using the Backslash Escape Sequence
One option is to use bash's escape character \. Any space following
the backslash is treated as being part of the same string. These commands
create a directory called "foo bar" and then remove it.
# mkdir foo\ bar
# rm foo\ bar |
The backslash escape sequence can also be used to decode commands embedded
in strings which can be very useful for scripting or modifying the command
prompt as discussed later.
Using Single/Double Quotes with Spaces and Variables
Single and double quotes can also be used for dealing with spaces.
# touch 'dog poo'
# rm "dog poo" |
The difference between single and double quotes being that in double
quotes the $, \, and ' characters still preserve their
special meanings. Single quotes will take the $ and \ literally
and regard the ' as the end of the string. Here's an example:
# MY_VAR='This is my text'
# echo $MY_VAR
This is my text
# echo "$MY_VAR"
This is my text
# echo '$MY_VAR'
$MY_VAR |
The string following the $ character is interpreted as being a
variable except when enclosed in single quotes as shown above.
Lists Using { and }
The characters { and } allow for list creation. In other words
you can have a command be executed on each item in the list. This is perhaps
best explained with examples:
| # touch {temp1,temp2,temp3,temp4} |
This will create/modify the files temp1, temp2, temp3, and temp4 and as in
the example above when the files share common parts of the name you can do:
| # mv temp{1,2,3,4} ./foo\ bar/ |
This will move all four of the files into a directory 'foo bar'.
Executing Multiple Commands in Sequence
This is a hefty title for a simple task. Consider that you want to run three
commands, one right after the other, and you do not want to wait for each to
finish before typing the next. You can type all three commands on a line and
then start the process:
# ./configure; make; make install
OR
# ./configure && make && make install |
I often use these formats in crontab files for commands that need to be executed
in sequence if I choose not to make a script.
Piping Output from One Command to Another
Piping allows the user to do several fantastic thing by combining utilities.
I will cover only very basic uses for piping. I most commonly use the pipe command,
|, to pipe text that is outputted from one command through the grep
command to search for text.
Examples:
See if a program, centericq, is running:
# ps ax | grep centericq
25824 pts/2 S 0:18 centericq
Count the number of files in a directory (nl counts things):
# ls | nl
1 #.emacs#
2 BitchX
3 Outcast double cd.lst
4 bm.shader
5 bmtexturesbase.pk3
If my memory serves using RPM to check if a package is installed:
# rpm -qa | grep package_name
A more advance example:
# cat /etc/passwd | awk -F: '{print $1 "\t" $6}' | sort > ./users
This sequence takes the information if the file passwd, pipes it
to awk, which takes the first and sixth fields (the user name and
home directory respectively), pipes these fields separated by a
tab ("\t") to sort, which sorts the list alphabetically, and puts
it into a file called users. |
Aliasing Commands
Once again I like how this topic is covered on freeunix.dyndns.org:8088 in
"Customizing your Bash environment" I will quote the section entitled "Aliasses":
If you have used UNIX for a while, you will know that there are
many commands available and that some of them have very cryptic names
and/or can be invoked with a truckload of options and arguments. So,
it would be nice to have a feature allowing you to rename these commands
or type something simple instead of a list of options. Bash provides
such a feature : the alias .
Aliasses can be defined on the command line, in .bash_profile,
or in .bashrc, using this form :
This means that name is an alias for command. Whenever
name is typed as a command, Bash will substitute command in its place.
Note that there are no spaces on either side of the equal sign. Quotes
around command are necessary if the string being aliassed consists of
more than one word. A few examples :
| alias ls='ls -aF --color=always' alias ll='ls -l' alias
search=grep alias mcd='mount /mnt/cdrom' alias ucd='umount /mnt/cdrom'
alias mc='mc -c' alias ..='cd ..' alias ...='cd ../..'
|
The first example ensures that ls always uses color if availabe, that
dotfiles are listed as well,that directories are marked with a / and
executables with a *. To make ls do the same on FreeBSD, the alias would
become :
To see what aliasses are currently active, simply type alias
at the command prompt and all active aliasses will be listed. To "disable"
an alias type unalias followed by the alias name. |
Altering the Command Prompt Look and Information
Bash has the ability to change how the command prompt is displayed in information
as well as colour. This is done by setting the PS1 variable. There is also a PS2
variable. It controls what is displayed after a second line of prompt is added and
is usually by default '> '. The PS1 variable is usually set to show some useful
information by the Linux distribution you are running but you may want to earn style
points by doing your own modifications.
Here are the backslash-escape special characters that have meaning to bash:
\a an ASCII bell character (07)
\d the date in "Weekday Month Date" format
(e.g., "Tue May 26")
\e an ASCII escape character (033)
\h the hostname up to the first `.'
\H the hostname
\j the number of jobs currently managed by the shell
\l the basename of the shell's terminal device name
\n newline
\r carriage return
\s the name of the shell, the basename of $0
(the portion following the final slash)
\t the current time in 24-hour HH:MM:SS format
\T the current time in 12-hour HH:MM:SS format
\@ the current time in 12-hour am/pm format
\u the username of the current user
\v the version of bash (e.g., 2.00)
\V the release of bash, version + patchlevel
(e.g., 2.00.0)
\w the current working directory
\W the basename of the current working direcory
\! the history number of this command
\# the command number of this command
\$ if the effective UID is 0, a #, otherwise a $
\nnn the character corresponding to the octal number nnn
\\ a backslash
\[ begin a sequence of non-printing characters,
which could be used to embed a terminal control
sequence into the prompt
\] end a sequence of non-printing characters
|
Colours In Bash:
Black 0;30 Dark Gray 1;30
Blue 0;34 Light Blue 1;34
Green 0;32 Light Green 1;32
Cyan 0;36 Light Cyan 1;36
Red 0;31 Light Red 1;31
Purple 0;35 Light Purple 1;35
Brown 0;33 Yellow 1;33
Light Gray 0;37 White 1;37
|
Here is an example borrowed from the Bash-Prompt-HOWTO:
| PS1="\[\033[1;34m\][\$(date +%H%M)][\u@\h:\w]$\[\033[0m\] " |
This turns the text blue, displays the time in brackets (very useful for not
losing track of time while working), and displays the user name, host, and current
directory enclosed in brackets. The "\[\033[0m\]" following the $ returns the colour
to the previous foreground colour.
How about command prompt modification thats a bit more "pretty":
| PS1="\[\033[1;30m\][\[\033[1;34m\]\u\[\033[1;30m\]@\[\033[0;35m\]\h\[\033[1;30m\]]
\[\033[0;37m\]\W \[\033[1;30m\]\$\[\033[0m\] " |
This one sets up a prompt like this: [user@host] directory $
Break down:
\[\033[1;30m\] - Sets the color for the characters that follow it.
Here 1;30 will set them to Dark Gray.
\u \h \W \$ - Look to the table above
\[033[0m\] - Sets the colours back to how they were originally. |
Each user on a system can have their own customized prompt by setting the PS1
variable in either the .bashrc or .profile files located in their home directories.
FUN STUFF!
A quick note about
bashish.
It allows for adding themes to a terminal running under a GUI. Check out the
site for some screen-shots of what it can do.
Also, the program fortune is a must [At least I have considered it
so every since my Slackware days (since Slackware included it by default)].
It doesn't have anything to do with bash and is a program that outputs a quote
to the screen. Several add-ons are available to make it say stuff about programming,
the xfiles, futurama, starwars, and more. Just add a line in your /etc/profile
like this to brighten your day when you log into your computer:
Basic and Extended Bash Completion
Basic Bash Completion will work in any bash shell. It allows for completion
of:
- File Names
- Directory Names
- Executable Names
- User Names (when they are prefixed with a ~)
- Host Names (when they are prefixed with a @)
- Variable Names (when they are prefixed with a $)
This is done simply by pressing the tab key after enough of the word you are
trying to complete has been typed in. If when hitting tab the word is not completed
there are probably multiple possibilities for the completion. Press tab again and
it will list the possibilities. Sometimes on my machine I have to hit it a third
time.
Extended Programmable Bash Completion is a program that you can install
to complete much more than the names of the things listed above. With extended bash
completion you can, for example, complete the name of a computer you are trying
to connect to with ssh or scp. It achieves this by looking through
the known_hosts file and using the hosts listed there for the completion. This is
greatly customizable and the package and more information can be found
here.
Configuration of Programmable Bash Completion is done in /etc/bash_completion.
Here is a list of completions that are in my bash_completion file by default.
- completes on signal names
- completes on network interfaces
- expands tildes in pathnames
- completes on process IDs
- completes on process group IDs
- completes on user IDs
- completes on group IDs
- ifconfig(8) and iwconfig(8) helper function
- bash alias completion
- bash export completion
- bash shell function completion
- bash complete completion
- service completion
- chown(1) completion
- chgrp(1) completion
- umount(8) completion
- mount(8) completion
- Linux rmmod(8) completion
- Linux insmod(8), modprobe(8) and modinfo(8) completion
- man(1) completion
- renice(8) completion
- kill(1) completion
- Linux and FreeBSD killall(1) completion
- GNU find(1) completion
- Linux ifconfig(8) completion
- Linux iwconfig(8) completion
- RedHat & Debian GNU/Linux if{up,down} completion
- Linux ipsec(8) completion (for FreeS/WAN)
- Postfix completion
- cvs(1) completion
- rpm completion
- apt-get(8) completion
- chsh(1) completion
- chkconfig(8) completion
- user@host completion
- host completion based on ssh's known_hosts
- ssh(1) completion
- scp(1) completion
- rsync(1) completion
- Linux route(8) completion
- GNU make(1) completion
- GNU tar(1) completion
- jar(1) completion
- Linux iptables(8) completion
- tcpdump(8) completion
- autorpm(8) completion
- ant(1) completion
|
- mysqladmin(1) completion
- gzip(1) completion
- bzip2(1) completion
- openssl(1) completion
- screen(1) completion
- lftp(1) bookmark completion
- ncftp(1) bookmark completion
- gdb(1) completion
- Postgresql completion
- psql(1) completion
- createdb(1) completion
- dropdb(1) completion
- gcc(1) completion
- Linux cardctl(8) completion
- Debian dpkg(8) completion
- Debian GNU dpkg-reconfigure(8) completion
- Debian Linux dselect(8) completion
- Java completion
- PINE address-book completion
- mutt completion
- Debian reportbug(1) completion
- Debian querybts(1) completion
- update-alternatives completion
- Python completion
- Perl completion
- rcs(1) completion
- lilo(8) completion
- links completion
- FreeBSD package management tool completion
- FreeBSD kernel module commands
- FreeBSD portupgrade completion
- FreeBSD portinstall completion
- Slackware Linux removepkg completion
- look(1) completion
- ypcat(1) and ypmatch(1) completion
- mplayer(1) completion
- KDE dcop completion
- wvdial(1) completion
- gpg(1) completion
- iconv(1) completion
- dict(1) completion
- cdrecord(1) completion
- mkisofs(8) completion
- mc(1) completion
- yum(8) completion
- yum-arch(8) completion
- ImageMagick completion
|
Links
-
Bash Prompt HOWTO
-
Bash Reference Manual
-
Customizing your Bash environment
-
Working more productively with bash 2.x
- Advancing
in the Bash Shell
-
Bash - Bourne Again SHell
-
What's GNU: Bash - The GNU Shell
-
Bash Tips in Gentoo Forums
-
bash(1) - Linux man page
-
BASHISH
Learn About Bash Scripting:
-
Bash by example, Part 1
-
Bash by example, Part 2
-
Bash by example, Part 3
-
Advanced Bash-Scripting Guide
-
A quick guide to writing scripts using the bash shell
If you want your xterm
or rxvt tille bar to show the username, hostname and current
directory and if you uses bash, you can set the PROMPT_COMMAND shell
variable. Personally, I use the following command in my /etc/profile:
if [ $TERM = "xterm" ]; then
export PROMPT_COMMAND='echo -ne \
"\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
fi
The test around the export command is done in order to avoid causing
problems in text terms.
|
| |
You can execute bash command
a certain number of times by using something similar to the following:
n=0;while test -$n -gt -10; do echo n=$n; n=$[$n+1]; done
That code will print "n=0", "n=1", and so on 10 times.
|
| |
| You can use CTRL-_
or CTRL-X, CTRL-U to make undo's at the bash prompt.
|
| |
| Bash supports tab-completion.
That is, you type the first few characters of a command (or file / directory)
and hit tab, and bash automagically completes it for you. For example,
if you wanted to run the program WPrefs (Window Maker prefrences
util), all you have to do is type WP<tab> and bash will fill
in the rest plus a trailing space. |
| |
| Hitting META-P in
bash will allow you to search through the bash history. |
| |
| If you find yourself having
to cd back and forth between long directory names, bash's pushd
is the perfect solution. Start in one of the directories, and the type
pushd directory2 to go to the second directory. Now if you type
dirs you should see the two directories listed. To switch between
these two directories just type pushd +1 |
| |
| While using bash, if you
have typed a long command, and then realize you don't want to execute
it yet, don't delete it. Simply append a # to the beginning of
the line, and then hit enter. Bash will not execute the command, but
will store it in history so later you can go back, remove the #
from the front, and execute it. |
| |
| In the bash shell, CTRL-U
will delete everything to the left of the cursor. |
| |
| CTRL-T in bash will
transpose two characters; great for typos. |
| |
| Hitting CTRL-W in
bash will delete the word just before your cursor. CTRL-Y will
yank back in the last deleted word (or words if they were delete consecutively).
If you deleted words after you deleted what you wanted to yank back
in, and already pressed CTRL-Y, you can use ALT-Y to look
through those words. |
| |
| Here's another way to change
into long directory names in bash. For example, the directory, samba-2.0.0beat2.
You can put in cd samb* and it will change to the directory that
matches the wildcard. |
| |
| In the bash shell, you can
utilize shortcuts. If your last command started with an l was
less xxx, then !l will re-execute it. However, if you
had been using lpr and ln as well, and you wanted to run
less again, then !le would execute it. |
| |
| In bash, hitting ALT-b will
move you back a word, and hitting ALT-f will move you forward a word.
|
| |
| Typeing CTRL-l at a bash
prompt, will clear the screen, and put the current line at the top of
the screen. |
| |
| Turning on the scrolllock
in a console will pause or suspend the current command in progress in
bash, such as ls, du or mpg123. |
| To lowercase files
in current $PWD #!/bin/sh for x in * do newx=`echo $x | tr "[:upper:]"
"[:lower:]";` mv "$x" "$newx" echo "$x --> $newx" done
|
| |
For one fast and
effective `clear' use echo e='\ec' It does more
that `clear' |
| |
| Finding out all the
commands installed on your box? At the prompt, press tab twice
and it will ask you if you want to see all the commands. Say
y and it will show you all the commands that you installed on
your box including shell syntax. Very easy to find out and to
familiar yourself with the commands you don't know (btw, this
only searches according to path variable set in bash login files).
But be careful if you are the root; try --help or man
page first before blindly type into it. If all the commands
listed are in single column and you can't see the top, edit
.bash_profile or .bashrc to include this alias: alias ls="ls
-C". Then you should be able to see all. One other alternative
might be to increase the buffer for the terminal so that it
will hold more characters. Hope this helps! |
| |
Would you like to
list only directories (without a long -l listing)?
dirs () {
ls -F $1 | grep \/ | sed -e 's/\/$//g'
}
Use 'dirs ' on your bash shell and enjoy!
|
| |
The readline support
in the bash shell defaults to emacs editing mode. You
can easily switch that to vi mode by issuing the following
command: set -o vi. |
| |
| If you use bash,
you can search backwards into its history: hit CTRL-R
and start typing what you want to search (it works exactly as
in Emacs). If there are lots of similar lines in your history,
repetedly typing CTRL-R will browse through them
|
| |
| bash allows you to
move between the current directory and the previous directory
using the hyphen after the cd command. Say you were in /home/john/pies/american.
You give the command cd /home/jack/steak/grilled Now
you could back to the ../../american directory using cd
- |
| |
you can clear the
screen when you logout, in bash, by adding this to the ~/.bash_logout
file:
setterm -clear
if you don´t have a .bash_logout file, just make one.
|
| |
I use cd bla;
ls -l bla so much I made a function for it see
function see () { cd $1; ls . ; }
|
| |
In bash, if you add
this:
complete -d cd
Into your ~/.bash_profile or /etc/profile
file, then when you cd, it will only search for directories.
So if you have a file called "jiggy" and a directory called
"joogy" and those are the only things in the directory, and
you type cd and press tab, it will just go into "joogy".
|
| |
| Under bash
or zsh, if you would like to edit a previous command
in a text editor instead of on the command line, use the
fc command. |
| |
Aliasing dir
to list just directories can be useful. To do so, do the following:
alias dir='ls -l | grep ^d'
grep in this case searches for a d in the first column
of each line. |
| |
| Another search-in-bash
thingy: CTRL+R, this is more "realtime"--when you enter
a char/string, it gives you a found match directly.
|
| |
If you want to switch
off the "beep" during command line-completion you should add
an entry either in your ~/.inputrc or system wide in
your /etc/inputrc:
for visual signal : set bell-style visible
for absolutely no signal: set bell-style none
|
| |
This is a variation
for the "colorful directory listing" hint users, that works
"better" under bash. Put the following in $HOME/.bashrc
or $HOME/.bash_profile:
function v () { ls -l --color=auto $*; }
function d () { ls --color=auto $*; }
HINT: Think of 'v' as "verbose" and 'd' as "directory". And
they're much quicker to type (only a single char), so this should
satisfy most unix junkies.
|
|
Copyright © 1996-2007 by Dr. Nikolai Bezroukov.
www.softpanorama.org was
created as a service to the UN Sustainable Development Networking Programme (SDNP)
in the author free time.
Submit
comments This document is an industrial compilation designed and created
exclusively for educational use and is placed under the copyright of the
Open Content License(OPL).
Original materials copyright belong to respective owners. Quotes are made
for educational purposes only in compliance with the fair use doctrine.
Standard disclaimer: The statements, views and opinions presented on
this web page are those of the author and are not endorsed by, nor do they necessarily
reflect, the opinions of the author present and former employers, SDNP or any other
organization the author may be associated with. We do not warrant the correctness
of the information provided or its fitness for any purpose.
Last modified:
April 02, 2008