Some useful Bash shell tricks
There are a lot of obscure Bash shell tricks for the Linux shell that is useful for showing off shell tricks and making your shell usage easier.
For a first example, run this command.
ubuntu ~ $ ls -hula |
Then run this and the previous command parameters will be applied again.
ubuntu ~ $ ls !:* |
This is one very cool trick.
To re-run a previous command, use this trick.
ubuntu ~ $ !! |
Or if you forget to use the sudo command.
ubuntu ~ $ sudo !! |
This is another way to rerun a command like ls -hula
.
!:0 !:* |
If there are 2 arguments to the command, then use this.
!:0 !:1-2 |
This also works, get the last executed command and all parameters.
ubuntu ~ $ !:t:r:p ls -hu -la |
Another way to add the previously used command-line parameter like -hula to the ls command is by pressing the Alt key and then the “.” key. This will paste the previously used command line parameters onto the command. If you keep pressing it, it will go back in history. Good if you wish to get parameters from 3 commands back.
Run this command on the bash shell, and the previous command will be run again.
ubuntu ~ $ !:h |
Another way to get a previously run command.
ubuntu ~ $ !!:0 ls 3aa21b8c106cab742bf1f20d60629e3f index.html openvpn 74567486745764985674876397573487563653874653875634865378653653485364753748653465348765348653865348756487365 index.html.1 out.gif account.sh ip2.c out.txt a.out ip.c pass.out bae64 ipinfo pentestlab.txt bandit14.txt jail results.xml base64 john.conf rockyou.txt boards.4chan.org key.key root.jpeg capture.log keys SAM chan.c libhd16_16.0-2.2_amd64.deb SECURITY clock.txt libhd21_21.6-1_amd64.deb shellcode.c crunch-3.6 linux-4.13.11.tar.xz sycall.c crunch-3.6.tgz linux-4.13.11.tar.xz.1 SYSTEM Documents loop.c test.gz easy-rsa mempodipper.c testingunit echo metrosexual.zip tmp gtl.out my2.text uptime.py guru.sh my.text uudecode hackme my.txt2 vim hash nmap-log-54.159.49.238-.txt vpn.sh hash.txt nmap-log-healthcare.gov-.txt xyz-wordlist.txt hwinfo_16.0-2.2_amd64.deb nohup.out id_rsa.pub nt |
Another way to rerun an old command is this way, it will search for and run the first command in the bash history that matches the search string.
┌──[jason@11000000.10101000.00000001.00000011]─[~] └──╼ ╼ $ !?ffmpeg? |
This example will run the last command in the .bash_history that starts with this string.
┌──[jason@11000000.10101000.00000001.00000011]─[/boot] └──╼ ╼ $ !ls |
This could be useful if you ran a command a while ago and then forgot most of it and you wish to run it again, this would be most helpful. Also, to get useful ASCII characters, use this manual page.
┌──[jason@11000000.10101000.00000001.00000011]─[/boot] └──╼ ╼ $ man 7 ascii |
This will print a very useful table of all ASCII characters.
This is a sample. it prints the numbers in Octal, Decimal, Hex and the Char as well.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | Oct Dec Hex Char Oct Dec Hex Char ──────────────────────────────────────────────────────────────────────── 000 0 00 NUL '\0' (null character) 100 64 40 @ 001 1 01 SOH (start of heading) 101 65 41 A 002 2 02 STX (start of text) 102 66 42 B 003 3 03 ETX (end of text) 103 67 43 C 004 4 04 EOT (end of transmission) 104 68 44 D 005 5 05 ENQ (enquiry) 105 69 45 E 006 6 06 ACK (acknowledge) 106 70 46 F 007 7 07 BEL '\a' (bell) 107 71 47 G 010 8 08 BS '\b' (backspace) 110 72 48 H 011 9 09 HT '\t' (horizontal tab) 111 73 49 I 012 10 0A LF '\n' (new line) 112 74 4A J 013 11 0B VT '\v' (vertical tab) 113 75 4B K 014 12 0C FF '\f' (form feed) 114 76 4C L 015 13 0D CR '\r' (carriage ret) 115 77 4D M 016 14 0E SO (shift out) 116 78 4E N 017 15 0F SI (shift in) 117 79 4F O 020 16 10 DLE (data link escape) 120 80 50 P 021 17 11 DC1 (device control 1) 121 81 51 Q 022 18 12 DC2 (device control 2) 122 82 52 R 023 19 13 DC3 (device control 3) 123 83 53 S 024 20 14 DC4 (device control 4) 124 84 54 T 025 21 15 NAK (negative ack.) 125 85 55 U 026 22 16 SYN (synchronous idle) 126 86 56 V 027 23 17 ETB (end of trans. blk) 127 87 57 W 030 24 18 CAN (cancel) 130 88 58 X 031 25 19 EM (end of medium) 131 89 59 Y 032 26 1A SUB (substitute) 132 90 5A Z 033 27 1B ESC (escape) 133 91 5B [ 034 28 1C FS (file separator) 134 92 5C \ '\\' |
So, that would also be very useful to someone.
Useful bash shell shortcut keys to navigate the command line