ArchLUG Kwiki - www.archlug.org

Download the RSS XML Feed for this site

Download the RSS XML Feed for this site

Subscribe to this channel with Amphetadesk

Subscribe to this channel with RadioUserland

Add to Google

CommandLineKwikis


For Users of Other OSes

If you're familiar with another Unix-like OS, there is an excellent tool for translating common system administration activities to the appropriate command to use for the task in that OS.

It is called The Rosetta Stone for Unix.

Help with Bash

http://www.ss64.com/bash/

For some Wicked Cool scripts

http://www.intuitive.com/wicked/wicked-cool-shell-script-library.shtml

Prompts

You can embed ANSI color and ANSI cursor motion codes into your prompt, however, this will likely not work or do strange things if you are using a different terminal type or emulation. You are better off capturing the values of these sequences using the 'tput' command.

set prompt = "%{^[[032;1m%}`whoami`%{^[[0m%} %c3 %B%#%b "

Fancy Bash Prompts

See Giles Orr's work on some fancy BashPrompts


Working more productively with Bash

Beyond LinuxFromScratch - After LFS Configuration - The Bash Shell Startup Files

Bash settings

set autolist        # automatically list possibilities after ambiguous completion
set dunique         # removes duplicate entries in the dirstack
set fignore=(\~)    # files ending in ~ will be ignored by completion
set histdup=prev    # do not allow consecutive duplicate history entries
set noclobber       # output redirection will not overwrite an existing file
set notify          # notifies when a job completes
set symlinks=ignore # treats symbolic directories like real directories
set time=5          # processes that run longer than $time seconds will be timed. 

Bash bindkeys

By default, the up and down arrow keys scroll up and down one at a time through the history. Instead, you can bind them to search the history based on what you've typed so far.

bindkey -k up history-search-backward  # up arrow key
bindkey -k down history-search-forward # down arrow key 

Bash Completes

Completes allow for customizing tab completion. When you change directories, tab will only complete directory names. This also works for aliases, sets, setenvs, etc.

complete cd 'p/1/d/'
complete alias 'p/1/a/'
complete setenv 'p/1/e/'
complete set 'p/1/s/' 

Bonus for Debian users

In Debian, the Bash package comes with a totally awesome collection of customized tab completions. However, they are not turned on by default. To turn them on in a single account, you can put the line "source etc/bash_completion" in your ~.bashrc file, or you can turn them on globally by editing the /etc/bash.bashrc file and uncommenting the relevant lines.

You'll get magic smart tab completion for cd, apt-get, ssh, mplayer, and numerous other programs. You'll wonder how you ever did without it. 'apt-get' tab completion in particular rocks like nothing else. For example, if you type 'apt-get remove x[TAB]' you'll get a complete list of installed packages starting with x. When installing, you'll get a list of available but not yet installed packages. You'll likely not be able to stand using apt-get without tab completion anymore. smile smiley

Bash Aliases

These are really all a matter of personal taste

alias cwdcmd 'ls'
alias precmd 'echo -n "\033]0;$USER@`hostname` : $PWD\007"'
alias pd 'pushd'
alias po 'popd'
alias dirs 'dirs -v'
alias path 'printf "${PATH:as/:/\n/}\n"'
alias ff 'find . -name '\''\!:1'\'' -print \!:2*'
alias aw 'awk '\''{print $'\!:1'}'\'''
alias sub 'sed "s@"\!:1"@"\!:2"@g"' 
alias ll 'ls -al'
alias lf 'ls -aF'

Bash Functions

# Combining 'ls' with 'less' and retaining color
function lsp () { ls --color "$@" | less -r; }

See PagingColorizedListings for details


Bash Options

cdspell

If you are using 'bash' shell, there is a way to 'cd' a particular directory even if you spelled incorrectly on the command line. Set the shell option to:

shopt -s cdspell

Example

Suppose you want to cd to "cd /tmp", and you have mis-typed it as "cd /pmp". With the cdspell shell option set, bash will silently correct the typo, and will cd to "/tmp" instead. As close as it gets to DWIMNWIT (Do What I Meant, Not What I Typed).

This setting is often very useful when you are dealing with a directory with a lengthy name. However, this is somewhat mitigated by the readline completion feature of bash.


checkwinsize

If you are using a terminal program (e.g. PuTTY) from a non-Linux computer, that has a useful feature to maximize or resize the terminal window when you need to see more of what's going on in that window.

When doing so, you will also want to inform the shell that your terminal size has changed, and to update the corresponding environment variables, COLUMNS and LINES, to reflect the new window size. Doing this manually is tedious, error-prone, often overlooked, and results in curses-based applications (e.g. vim) and applications that format their output for the screen (e.g. man or diff) producing output for a different sized window than you are looking at.

To have 'bash' check the current terminal size and update COLUMNS and LINES do this for you automatically before each command, set the following shell option:

shopt -s checkwinsize

How to capture a console session

For capturing the output of those commands that spew forth far more than your measly scrollback allocation...

See ManPage : script

Note: This starts a subshell, and preserves backspaces and terminal control codes.


sftp, .tcshrc, and other login issues

Secure FTP (sftp) and some other logins fail if a login script writes or resets the virtual terminal.

Such logins tend to not set the TTY or TERM environment variables. So .tcshrc might specify:

if ( $?term == 1 ) /bin/bash --login

Specify what you want to occur when you login to issue commands. This particular example is handy if you cannot chsh, ypchsh, or otherwise change your default shell. An "exec" as in "exec /bin/bash --login" does not start a subshell, but can cause login processes besides sftp to fail.



Valid XHTML 1.0! Valid CSS!
InterTran (www.tranexp.com)
InterTran (www.tranexp.com)

Please MOVE AND HOLD your MOUSE CURSOR over any WORD in the translated web page in order to see a pop-up window with ALTERNATIVE TRANSLATIONS. Translations provided by: www.tranexp.com