Wednesday, January 20, 2016

Monkeying around on the bash command line in 2016

http://www.computerworld.com/article/3018637/open-source-tools/monkeying-around-on-the-bash-command-line-in-2016.html


Year of the Monkey
Soon it will be 2016 -- the Year of the Monkey in the Chinese Zodiac's 12 year cycle. People born in these years (e.g., 1920, 1932, 1944, 1956, 1968, 1980, 2004, and now 2016) are supposed to be quick-witted, optimistic, ambitious, etc. So, let's see what quick-witted, optimistic, ambitious things we can do to monkey around on the command line more gracefully.
To start, let's look at some of the more unusual and less obvious things that you can do with Linux history. The tricks that most command line users know include these relatively easy commands:
Example   Description
=======   ===========
!e        run the last command you ran that started
          with "e"
!22       run the 22nd command as stored in the
          history commands
!!        run the previously entered command
sudo !!   run the previous command using sudo (very
          helpful if you forgot to use sudo and don't
          want to retype the entire command)
sudo !e   run the last command you ran that starting
          with "e" using sudo
sudo !22  run the 22nd command in your history using
          sudo
Less obvious are commands such as !-2 that run previous commands based on how far we have to reach back in our history. This one runs the command that was entered two commands ago (i.e., the command before the most recent command).
$ echo one
one
$ echo two
two
$ echo three
three
$ !-2
echo two
two
Command line substitution commands can also be very helpful. One that I find fairly useful is !$ which allows you to reuse the last string from the previous command. Here's an example where the argument is a fairly long path:


# ls /home/jdoe/scripts/bin
showmypath  trythis
# cd !$
cd /home/jdoe/scripts/bin
Here's another example that might make it easier to see that only the last of three arguments is reused.
# echo one two three
one two three
# echo !$
echo three
three
# echo one two three
one two three
# echo !^
echo one
one
Clearing history can be useful if you want to focus on just recent commands, though this doesn't clear out your .bash_history file. You should also remove the file if you want your command history removed from the system.
# history -c
# history
  238  history
Some other interesting options for history commands include using HISTCONTROL settings. These allow you to ignore commands entered with preceding blanks, ignore duplicate commands (when they've been entered consecutively), and do both.
HISTCONTROL=
  ignoredups ignore duplicate commands
  ignorespace ignore commands starting with spaces
  ignoreboth ignore consecutive duplicates and commands starting with blanks
HISTSIZE set the size of your history queue
The first of these (HISTCONTROL=ignoredups) ensures that, when you type the same command multiple times in a row, you will only see one of the repeated commands in your history file. Notice how the pwd command appears only once in the history output though we entered it three times.


# pwd
/home/jdoe/bin
# pwd
/home/jdoe/bin
# pwd
/home/jdoe/bin
# echo hello
hello
# history | tail -4
  249  HISTCONTROL=ignoredups
  250  pwd
  251  echo hello
  252  history | tail -4
The second option (HISTCONTROL=ignorespace) means that, when you start a command with blanks, it won't be stored in your history file. This can be very useful when you want to run commands that you don't want recorded (e.g., when they include a password).
#      echo do not store this command in my history
do not store this command in my history
# echo ok
ok
# history -3
  244  clear
  245  echo ok
  246  history -3
The third (HISTCONTROL=ignoreboth) option sets up both of these history control settings.
The HISTSIZE setting adjusts the size of your history queue. Unless you make this change permanent by putting in one of your dot files (e.g., .bashrc), the setting won't persist and your history queue will go back to its original size.
The following command line options are useful, but hard enough to remember that many of these changes might be easier to do manually than trying to store these tricks in your head. And keep in mind that the position of the cursor on the command line often determines what each of the commands will do. Fortunately, there's an undo command to reverse any change you just made.
ctl+a    move to the beginning of the command line
ctl+e    move to end of line
alt+f    move to space following the end of the word
alt+b    move to start of current or previous word (if you're in
         the space)
ctl+t    swap 2 characters (the current and preceding)
alt+t    swap the current and previous words
ctl+u    cut text before cursor
ctl+w    cut part of the word before the cursor
ctl+k    cut text of current command after the curssor
ctl+y    paste the cut text after the cursor
alt+u    uppercase the next word or remaining part of current
         word (curson and on)
alt+l    lowercase the next word
alt+c    capitalize the next word
ctl+L    clear the screen
ctl+_    undo the change you just made

In all of these command line tricks, alt+ means hold down the alt key and then type the letter that follows while ctl+ mans hold down the control key. I show the subsequent letters all in lowercase since you don't need to use the shift key.
Position your cursor on the "three" in this echo command and you'll see the two and three swapping places:
$ echo one two three four five
one two three four five
$ echo one three two four five
While these are all nice options for manipulating your commands, you might find that many are just not worth trying to keep all the alt and ctl sequences straight. Maybe several will come in handy depending on the command line changes you frequently need to make.

No comments:

Post a Comment