[Nottingham] Bash commands "hints 'n' tips"

Martin martin at ml1.co.uk
Fri Jan 20 00:58:39 GMT 2006


As promised at Wed's excellent talk by Michael, here's a few bash-isms 
from an amazingly long time ago!

Anyone want to add their favourites?

Enjoy,
Martin



-------- Original Message --------
Subject: Re: [Nottingham] bash prompt
Date: Thu, 10 Jun 2004 15:55:51 +0100
From: Martin <martin at ml1.co.uk>


Martin wrote:
 > Martin Garton wrote:
[...]
 >>
 >> export PS1="$PS1 \`if [ \$? = 0 ]; then echo \:\); else echo \:\(; fi\`"
 >>
[...]

A small tweak (hack into your Mandrake /etc/bashrc):

[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W :\`if [ \$? = 0 ]; then
echo \); else echo \(; fi\`]\\$ "

(All on one line!)

Or (inspired by J.G.):

[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W :\$([ \$? = 0 ] && echo \)
|| echo \()]\\$ "

(All on one line!)


If non-Mandrake or using your ~/.bashrc, you'll likely need to drop the
"[ ... ] &&" test.



-------- Original Message --------
Subject: Re: [Nottingham] Bash commands
Date: Tue, 10 Feb 2004 11:52:44 +0000
From: Martin <martin at ml1.co.uk>


A small update to the bash tips file.

Any volunteers for the input.rc stuff?

Any other goodies that people find useful for their systems and workings??

(And all to be added to James' wiki-ings...)

Until the follow-on talk on bashing scripts...

Cheers,
Martin


Martin wrote:
[...]
> 
> As promised in the talk:
> 
>  > Is there any chance the link for the
> 
>> talk could make it onto the list for all to see?
> 
> 
> See:
> http://www.ukuug.org/events/linux2003/papers/bash_tips/index.html
> 
> It also helpfully includes side notes that expand on the slides.
> 
> 
>> I promised that I'd send the solution to one bash problem which seems 
>> to be quite commonplace. The problem is that your command line doesn't 
>> overflow to the next line properly and will overwrite what you have 
>> already typed. This is caused by resizing the bash window without it 
>> realising.
>>
>> The solution is very easy, just add the following line to the 
>> appropriate place - .bashrc or wherever is best for your distribution. 
>> Personally I have it in /etc/profile (on Gentoo) so that it is default 
>> for every user.
>>
>> The line:
>>
>> shopt -s checkwinsize
>>
>> Another of my favourite aliases is "lsd", which I define to get a list 
>> of directories within the current directory. It does only work for the 
>> current directory, so lsd /etc wouldn't work, but it would be quite 
>> easy to modify it to accept arguments.
>>
>> alias lsd='ls -l | grep ^d'
> 
> 
> I'll let the brave experiment with the above.
> 
> The even braver can play with this below:
> 
> $cat /etc/profile.d/mlbashtips.sh
#
# M Lomas 'bash Hints and Tips'
# 2004/02/10 mlcodeN ATo ml1s cop uka zzm
#
# Summary of Simon Myers presentation at:
#
# UKUUG Linux 2003 Conference ? August 2003
# http://www.ukuug.org/events/linux2003/
#
#

# Firstly, check that this really is bash!

[ "X$SHELL" != "X/bin/bash" ] && exit


# Make Bash append rather than overwrite the history on disk
shopt -s histappend

# Whenever displaying the prompt, write the previous line to disk
PROMPT_COMMAND='history -a'

# Functions: These have the advantage over alias of letting
# you manipulate command line entered arguments, and of
# permitting more complicated sequences of instructions
#
# Define ls function providing default option
# (Commented out unless you want this.)
#function ls
#{
#  command ls -F "$@"
#}
#export -f ls

# New function example - duff, a variation of diff
# - Leaves the original name alone for programs relying on it
function duff
{
   diff -ur "$@"
}
export -f duff

# Further useful new functions:

# Long format ls
#function ll
#{
#  command ls -Flh "$@"
#}
#export -f ll

# Pushd and list the docs for a package
function doc
{
   pushd "/usr/share/doc/$1" && ls
}
export -f doc

# Shortcut for using fig2ps
#function fig2ps
#{
#  fig2dev -L ps "$1" "${1%.fig}.ps"
#}
#export -f fig2ps

# Saves [forgetting] having to type the & every time
#function gimp
#{
#  command gimp "$@" &
#}
#export -f gimp


# Bash will cope with each component of the typed path
# having one missing character, one extra character,
# or a pair of characters transposed:
#shopt -s cdspell

# set the cd search path
#export CDPATH='.:..:../..:~/links:~:~/projects:/var/www/virtual_hosts'
export CDPATH='.:..:../..:~/Documents'

# Bash's programmable completion enables commands' arguments
# to be completed intelligently for different commands
#source /etc/bash_completion

# To disable autocomplete for a particular command, eg: "cd"
#complete -r cd

# Specify that ctl-d must be pressed twice to exit a shell:
# Avoid unwanted exit due to over-twitchy fingers!
#export IGNOREEOF=1


# Note:
#
# Downside of such luxuries is that that sometimes
# you have to use systems where they aren't configured
# Dangerous if you get complacently used to 'interactive'
# features for cp, mv, rm etc...

# M Lomas short aliases list:
alias d="ls"
alias l="ls"                    # classical listing
alias ll="ls -l"                # List detailed
alias la='ls -a'                # List all
alias lsd="ls -d */"            # List only the directory
alias cd..="cd .."              # Catch the missing space
alias s="cd .."                 # 'super' or 'shift to' (parent) dir
alias p="cd -"                  # Previous dir

alias md="mkdir"                # Abbreviated mkdir
alias rd="rmdir"                # Abbreviated rmdir
alias cp="cp -i"                # Interactive overwrites (note danger!)
alias mv="mv -i"                # Interactive overwrites (note danger!)
alias rm="rm -i"                # Interactive deletes (note danger!)

alias m=more                    # Or should this be less?...
alias lla='ls -la'              # List detailed including '.' files
alias llh='ls -lh'              # List detailed with Human numbers
alias ld="ls -d */"             # List only the directory
alias dfh='df -h'               # List fs with Human numbers
alias v='vim'                   # Or should this be vi?...
alias hh='history'              # List the history

#
# Any additions?
#
# Enjoy, Martin.
#



-- 
----------------
Martin Lomas
martin at ml1.co.uk
----------------



More information about the Nottingham mailing list