home | changes | index | login

Bash Tricks

WareLogging. SourceCode. BASH is the BourneAgainShell. In terms of scripting, I tend to think it's considerably more sane to write Perl these days, but I promise you Bash is not going away any time soon.

= keys & builtins =

= references & tutorials =

= aliases =

To investigate: BashFunctions.

From several versions of ~/.bashrc:

# stack-based directory navigation:
# c to change, b for back, u for up.
alias b='popd'
alias u='pushd ..'
alias c='pushd'

# frequent commands:
alias m='mutt'
# p used to be pine, but I quit using pine.
alias p='ssh p1k3.com'
alias v='vim'

# these might break using GNU date(1), I should really check.
alias t='cd /www/p1k3/archives/`date +%Y`/`date +%m`'
alias tv='vim /www/p1k3/archives/`date +%Y`/`date +%m`/`date +%d`'

# colorized & shortened ls
alias ls='ls --color=auto '
alias ll='ls -lh'
alias la='ls -A'
alias l='ls -CF'

# for those MS-DOS kinda days
alias dir='ls --color=auto --format=vertical'
alias vdir='ls --color=auto --format=long'

# command prompt
# user@host:working_directory$
PS1='\u@\h:\w\$ '

= .inputrc =

This stuff is crucial: Kill the obnoxious beep, and cycle through possible completions rather than requiring 2 presses of the tab key to display a list of choices.

set bell-style none set completion-ignore-case on set completion-query-items 150 set mark-directories on set visible-stats on TAB: menu-complete

= Constantly refreshing display of command output =

I often want to monitor a file or service in real-time. This lets me do it:

while 1 ; do { clear; tail -20 /var/log/messages; sleep 1; } done

This will display the last 20 lines of /var/log/messages on a blank screen, refreshing every second. Replace "tail -20 /var/log/messages" with the command that you want to refresh.

<Brennen> As another approach, tail(1) itself offers the -f / --follow option, so for example,

tail -f /var/log/httpd/access.log

At least some versions of tail support following multiple files at once. These may be GNU specific options; I'll look it up at some point.

There's also watch(1), which is essentially the above loop for whatever command you feed it.

<Brent> Very cool! I created the above trick because I've been on systems on which watch wasn't available, and I wanted to watch a process (say, "ps -ef | grep sql") instead of a file.

It's also nice to have it clear the screen each time.

= In-place Files =

In attempting to automated FTP uploads, Brent has had trouble passing data to the ftp command. An easy solution:

ftp -n -i (HOST) <<- endcommands USER (USERNAME) (PASSWORD) cd wwwroot mput * bye endcommands

The -n parameter suppresses auto-login, and -i turns off prompting.

pick a name (required to comment or edit a page)
last edited December 30, 2009