Last Updated: February 25, 2016
·
362
· Chao Yang

Some Shell tips.

a. "!$" -- the very last parameter or if no parameter in last command it will return last command, example:

$ cat /etc/vpnc/default.conf
$ echo !$
echo /etc/vpnc/default.conf
/etc/vpnc/default.conf

"alt" + "." has the same effect to do this, which I usual use. And also "Esc" + "." . The difference between "!$" and "Alt/Esc" + "." is that, "!$" only contains one variable, but "Alt/Esc" + "." contain something like history, you can type multiple times for go back and back again.

b. "cd -" -- return to the previous directory.

$ pwd
/root
# cd /etc/sysconfig/network-scripts/
$ pwd
/etc/sysconfig/network-scripts
$ cd -
/root
$ pwd
/root

c. "^$old^$new" -- replace the old with the new content.

$ echo wonderfull
 wonderfull
 $ ^full^ful
 echo wonderful
 wonderful

d. ":> $filename" -- create a new file or if the file already exists blank it.

$ ls
 backup
 $ :> testfile
 $ ls
 backup testfile
 $ echo 111111111111111 > testfile
 $ cat testfile
 111111111111111
 $ :> testfile
 $ cat testfile
 $

It can create/clean $filename without warning, or if you want to get some error message if there is the same file, use touch, I guess. Seems ":>" & ">" doing the same, but what's the difference ?