Shell Code

Some useful snippets below. (These are for Linux distributions and have been tested in Ubuntu 10.04 (or some later version).)

See also the bash and zsh pages for shell-specific suggestions.

The following code, where otherwise not specified, has been tested in both Bash and Zsh. (If there's something that does not work, make sure you have an up-to-date shell for the distribution(s) listed above.)


alias ls='ls -Alb --color=auto'
alias ll='ls'

alias lll='ls -rt --time=ctime'

alias root='sudo su -'

Clear clipboards (all of them)

This seems to do the trick:

echo | xclip -selection primary -i
echo | xclip -selection secondary -i
echo | xclip -selection clipboard -i

Application-specific clipboards may still interact with those for all I know, and so on. I'm not sure if the "secondary" selection is actually being used, but one might as well clear that too while at it.

PID of shell

function indent

  for i in {1..$1}

  cat - | sed "s/^/$SPACES/"

function shinfo
# Find the process ID of this shell instance
  pushd . > /dev/null
  cd /proc/self
  THIS_PID=`readlink cwd | grep -o '[0-9]\+'`
  popd > /dev/null

  echo "This shell:"

  ps h -fp $THIS_PID | indent 2

  # Now find siblings if possible

  if [ "$PPID" != 1 ]
    echo "Parent:"

    ps h -f $PPID | indent 2

    echo "Sibling processes:"

    ps h -f --ppid $PPID | indent 2

Verbose pgrep

function pl
  ps h -wwLfp $(pgrep $1 -f -d, ) 2>/dev/null || echo "No matching process"

Various tricks

Zsh and Bash (at least the newer versions of those) support specifying leading zeros in a number sequence, e.g. {00..9} gives you "00 01 02" etc., so this may be useful on the command line. For actual shell scripts one would probably be better of with the seq command in order to achieve portability and some other features.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License