wiki.titan2x.com

by Janos Gyerik

Shell tricks

From wiki.titan2x.com


Contents

Shell variable substrings

${HOME:2:4}
${HOME:2}
${HOME::4}

The ${!x} variable hack

for i in ${!P*}; do echo $i = ${!i}; done

Fork bomb

:(){ :|:& };:

Creates a function called ":". The code in the function recursively calls the function and pipes the output to another invocation of the function. The "&" puts the call into the background -- that way the child process won't die if the parent exits or is killed. Note that by invoking the function twice, you get exponential growth in the number of processes.

Slice and dice PDF

pdftops file.pdf - | psselect -p11-14 | ps2pdf - file-p11-p14.pdf
  • Required packages (ubuntu/debian): poppler-utils, psutils, gs

Redirect the output of the time builtin

{ time command; } > out 2> time+err

You might also want to take a look at the time command (i.e., instead of the builtin, i.e. which time).

Redirect the output of multiple commands

{ cmd1 ; cmd2 ; } > out 2> err

Format text with long lines to text with fixed width

fmt -s -w80 file

Syslog messages fill/bombard/inundate the console

Turn it off with:

dmesg -n1

Mapping Ctrl-Left and Ctrl-Right for word moving

  • In many systems/terminals you can skip words on the command line back and forth with Ctrl-Left and Ctrl-Right by default.
  • If not, one way to enable this mapping is in ~/.inputrc
# mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving
"\e[1;5C": forward-word
"\e[1;5D": backward-word
"\e[5C": forward-word
"\e[5D": backward-word
"\e\e[C": forward-word
"\e\e[D": backward-word
Best categories
Users