Skip to content

Bash shell script tips and tricks

Janos Gyerik edited this page May 2, 2013 · 1 revision

Table of 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

Other, random

  • fc - opens last command in $EDITOR
  • disown - bash builtin to disown a process so it won't get a SIGHUP when exiting the shell
  • lsof. Surprised no one has mentioned it before. Shows open file handles, sockets, etc.
    • lsof -n
    • kill -HUP `lsof -t -c sshd`
  • fuser
  • man ascii
  • !:gs/foo/bar
  • sudo !! - repeats last command with sudo prepended
  • fmt - simple optimal text formatter
  • pgrep/pkill - even had an alias to do the same before I knew about it
  • python -m SimpleHTTPServer 8080
  • xxd
  • pstree
  • ddate
  • diff <(sort samples1.txt) <(sort samples2.txt)
  • :read !<command></command>
  • pr - allows you to eg combine many single column text data files into one
  • readlink - display value of a symbolic link
  • gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=output.pdf -dBATCH 1.pdf 2.pdf 3.pdf 4.pdf
  • strace, ltrace
  • !! - substitute prev command to current line
  • Use !* to refer to all arguments in the previous command.
 # todo: read up this, can be really practically useful!
 bind '"\e[A":]history-search-backward'
 bind &#39;&quot;\e&#91;B&quot;&#58;&#93;history&#45;search&#45;forward&#39;

bash hackers

F21&ut=3DWP-BEKD3oB01 Sure! So, for any basic transfer of a file, you do nc -lp 1234 >foobar on the receiving computer, where 1234 is the port number (you can't bind ports under 1024 as a regular user), and foobar is the name of the file. Now on the computer you want to transfer from, you do nc 192.168.0.1 1234 <foobar></foobar>:)
Clone this wiki locally