In bash you can place redirections almost anywhere in the command. All these are equivalent:

$ > file command arg1 arg2 arg3
$ command > file arg1 arg2 arg3
$ command arg1 > file arg2 arg3
$ command arg1 arg2 > file arg3
$ command arg1 arg2 arg3 > file

Try it yourself:

$ > file echo hello
$ echo > file hello
$ echo hello > file

Pretty cool, huh?

Six Years of BloggingHey everyone! Another year has passed and it's been 7 years since I've been blogging here at catonmat.net! In this post I want to summarize this year's statistics.

See the one year of blogging, two years of blogging, three years of blogging, four years of blogging, five years of blogging, and six years of blogging for the previous years' statistics.

Let's start this year's stats with traffic:



Traffic statistics from Google Analytics for Jul 1, 2013 - Jun 30, 2014.

This year: 784,992 uniques. 983,890 visits. 1,376,933 page views.
Last year: 903,468 uniques. 1,191,596 visits. 1,695,544 page views.
Delta: -118,476 unqiues. -207,706 visits. -318,611 page views.

I haven't been blogging much at all and that explains the negative delta. I've about 200 articles in drafts so stay tuned for some quality articles soon!

Feedburner stats: At 16,500 rss subscribers this year. Last year 16,343. Delta 157. subscribe.

Feedburner is a dead service now, so the rss subscriber count has become a meaningless, random number that changes daily. It fluctuates around 16,500 subscribers.

Github stats: At 1700 followers this year. Last year 1400. Delta +300. follow me.

Twitter stats: At 4130 followers this year. Last year 3774. Delta +356. follow me.

Total articles written: 20. Top 10 articles:

My top 5 favorite articles:

That's all folks! Now let's have some 7 year birthday cake,

And let's meet for the cake next year again! See you!

Someone asked me today about Perl array operations and it turned out he didn't know that all basic array operations, such as push, pop, shift, unshift can be implemented with just splice. So I thought I'd write a blog post that shows how awesome splice is.

splice

Splice has the following prototype:

splice(array, [offset, [length, [list]]])

Splice replaces length elements in array at offset with list. Offset, length, and list can be omitted, which makes it a really powerful function that can do a bunch of array operations.

push via splice

To push to an array via splice, you need to set offset to the end of the array, length to anything, and list to the list of values that you want to add.

Let's say you've array @a:

my @a = ("foo", "bar", "baz", "rms", "lbt", "esr");

Then you can push to it via splice like this:

splice(@a, @a, 0, "val1", "val2", "val3")

The @a array now contains:

("foo", "bar", "baz", "rms", "lbt", "esr", "val1", "val2", "val3")

pop via splice

To pop an element from an array via splice, you need to set offset to the end of the array.

Let's say you've array @a:

my @a = ("foo", "bar", "baz", "rms", "lbt", "esr");

Then you can pop it via splice like this:

splice(@a, -1)

The @a array now contains:

("foo", "bar", "baz", "rms", "lbt")

Here splice replaced the last element of the array with nothing, effectively removing it.

shift via splice

To shift a value off an array via splice, you need to set offset to the beginning of the array, and length to 1.

Let's say you've array @a:

my @a = ("foo", "bar", "baz", "rms", "lbt", "esr");

Then you can shift a value like this:

splice(@a, 0, 1)

The @a array now contains:

("bar", "baz", "rms", "lbt", "esr")

Similarly you can shift more than one value, if you increase length:

splice(@a, 0, 5)

This shifts first 5 values off the original array and it now contains:

("esr")

unshift via splice

To unshift values to an array via splice, you need to set offset to the beginning of the array, length to 0, and list to the list of values that you want to add.

Let's say you've array @a:

my @a = ("foo", "bar", "baz", "rms", "lbt", "esr");

Then you can unshift a new list to this array like this:

splice(@a, 0, 0, "val1", "val2")

The @a array now contains:

("val1", "val2", "foo", "bar", "baz", "rms", "lbt", "esr")

replace i-th element in an array via splice

To replace i-th element in an array via splice, you need to set offset to i, length to 1 and list to the list of values.

Let's say you've array @a:

my @a = ("foo", "bar", "baz", "rms", "lbt", "esr");

Then you can replace the 2nd element ("bar") via splice like this:

splice(@a, 1, 1, "ror")

The @a array now contains:

("foo", "ror", "baz", "rms", "lbt", "esr")

Similarly you can replace the i-th element with a list of values:

splice(@a, 1, 2, "ror", "zoz")

The @a array now contains:

("foo", "ror", "zoz", "baz", "rms", "lbt", "esr")

There are more operations that you can do with splice, such as deleting all elements in the array, adding elements at the i-th position. I'll leave these other operations as an exercise to the reader.

I think you can do the same in other languages as well, such as JavaScript, as it also has array.splice operation.

How often have you accidentally pressed ctrl+s and had your terminal frozen? I don't understand this default mapping at all. It should be gone from Linux installations. If you're a newbie you might have even thought that the terminal has died (I did think so, and I'd restart the terminal when that happened). Apparently you need to press ctrl+q to unfreeze the terminal. These keys could be used for something better than freezing and unfreezing the console.

Get rid of these keys by running these commands:

stty stop undef
stty start undef

Instead remap these combinations to something much more useful, such as saving the file in vim with ctrl+s.

Here's another pro tip. Say you wanted to run a command in sudo context but forgot that. Usually you'd hit the up arrow, press ctrl+a, or HOME button, type sudo and hit ENTER.

There is a geekier way to do that. Just type:

$ sudo !!

Bash will expand the two exclamation points to the previous command and it will be run through sudo.