bash

My bashrc, bash aliases, profile and other files

19 October, 2009 - 15:30
Categories:

I spend a lot of time on the (bash) command line in (Linux and OS x) and it's always good for productivity to have your favorite set of shortcuts, aliases, perferences, predefined (environment) variables and such at your fingertips. You know, all the stuff that lives in .bashrc, .profile, .vimrc, .git, etc.

I have access to several systems (at home, at work, remote servers, etc) and from time to time I'm sitting behind a system that doesn't know my preferences yet. I considered using some sort of version control system to "synchronize" these preference files between my setups, but (apart from the practical hurdles) it generally seemed overkill, especially since each setup can have its own peculiarities and customizations.

So, mainly for my own reference, I'm keeping an list of the most important general stuff here. Be welcome to cherry-pick (or suggest additions).

Read more...

Jython and PYTHONPATH

9 September, 2009 - 11:07
Categories:

For some small tools or jobs I use Jython so I can (re)use some existing Java code, while still writing in Python. I also have various custom Python modules, of which I put the paths in my PYTHONPATH environment variable, so those modules are easily available in Python scripts and interactive sessions.

However, Jython does not automatically pick up the PYTHONPATH information, for reasons that seem to be discussed in this Jython development mailing list thread.

Jython 2.5 introduced the JYTHONPATH environmental variable as Jython-equivalent of PYTHONPATH, so setting both to the same value should do the trick for most use cases (unless you're working in a setup with incompatible Python an Jython versions).

For Jython versions prior to 2.5 (like 2.2.1 in my case), there is an easy "workaround", by invoking Jython with the appropriate -Dpython.path=foo/path:bar/path option. For example as follows:

Read more...

Showing the running command in a bash script with "set -x"

29 May, 2009 - 09:03

Bash scripts are handy for putting a bunch of longer running jobs in one batch. In some occasions it can be useful to know which command is running at the moment, especially when the commands do not generate output themselves or this output is redirected to files.

Bash has a simple setting to achieve this:

set -x

which, according to help set, enables the following feature:

-x Print commands and their arguments as they are executed.

Disabling it just takes a mere

set +x

You can play with it from the bash command line itself, or put it in a bash script of course.

Read more...

Bash: the difference between $* and $@ and what that means for working with filenames containing spaces

29 November, 2005 - 19:39
Categories:

Lets's start with a citation about the special parameters $* and $@ from the bash manual:

Read more...

cat with rot13 encoding

27 November, 2005 - 17:46
Categories:

If you're familiar with the Unix or Linux commandline you probably know cat for printing files to standard output. Rot13 is a simple text encryption/cyphering/obfuscation (or whatever you want to call it) technique. It replaces each letter with the letter 13 places further in the alphabet. For example: 'a' becomes 'n', 'z' becomes 'm' and 'n' becomes 'a' (which illustrates the fact that the rot13 operation and its inverse are the same).

Read more...

shell redirection of standard output and standard error to the same file

22 November, 2005 - 11:49
Categories:

Consider a program hello_and_error that writes to both stdout and to stderr. To redirect both to stdout:

hello_and_error 2>&1

If you want to redirect both to the same file hello.txt you can do in bash the following:

hello_and_error &> hello.txt

If you want to do this in a more obvious (but longer) way you should do

hello_and_error 1>hello.txt 2>&1

and not

hello_and_error 2>&1 1>hello.txt
Read more...

Bash: about .bashrc, .bash_profile, .profile, /etc/profile, etc/bash.bashrc and others

3 October, 2005 - 17:08
Categories:

Ever wondered what's the difference between ~/.bashrc, ~/.bash_profile, ~/.profile, /etc/profile, /etc/bash.bashrc (and maybe others) and what their purposes are? I do.

Some interesting excerpts from the bash manpage:

Read more...

Avoid duplicates in your bash history

3 October, 2005 - 15:56
Categories:

Sometimes I need to repeat a command several times on the bash commandline. I don't mean a scenario where you could use a for or while loop, but take for example editing a script in an editor and periodically running it in a terminal. In that case my bash history used to be appended over and over with the same command for running that script.

If you set the shell variable HISTCONTROL to ignoredups, bash will not append a command to the history if it matches the last command of the history.

Read more...

Get the last argument of the previous command in the bash shell

11 August, 2005 - 12:34
Categories:

Working on the command line shell typically involves using the same argument of the previous command. In bash there is a trick to save you those boring keystrokes. When you use "!$" (called bang dollar-sign) in a command, bash will expand it to the last argument of the previous command. A simple example to clarify:

$> file /etc/fstab
/etc/fstab: ASCII text
$> ls -l !$
ls -l /etc/fstab
-rw-r--r--  1 root root 894 2005-08-03 15:12 /etc/fstab
Read more...

walking through directories with pushd and popd

6 August, 2005 - 13:28
Categories:

When you're spending a part of your life on the commandline, you'r probably cd'ing a lot in and out directories. Some handy commands (for bash):

  • cd (without arguments) brings you to your home directory
  • cd - (with just a minus) brings you to the previous directory you went to with cd
Read more...