Wednesday, May 31, 2017

Windows and Linux command recall compared

I prefer to use Linux, but sometimes I need to use Windows. What is frustrating is that sometimes the operating systems have essentially the same feature, but there are differences in implementation that can trip you up if you forget about them.

Often when you are working at the command line you will need to issue the same command  you used before.(or a similar command). With either Windows or Linux you can easily recall the last command by typing up arrow once or recall the command before that by typing up arrow twice. So far so good, but if you read at the finer details of command recall on Linux or Mac you can see there are quite a few differences to the way the feature is implemented on Windows.

Mostly the differences are different key combinations used which can be difficult to remember when you switch between both systems. However, there are a few things that are significant:
  1. The way recalling commands affects your place in history:

    Linux takes a simple view of command history, while Windows adjusts your perception of history depending upon whether you typed a command directly or recalled it from history. This is a tricky concept, so lets look at example. Suppose you had typed the following commands:

     > command a
     > command b
     > command c

    If you type up-arrow twice and then return, both Windows and Linux would execute 'command b' because this is the second last command, However, the two systems have a subtly different view of your command history after this. Linux takes the view that you typed command a/b/c/b and so if you type up-arrow twice you should get 'command c'. Windows agrees that 'command b' was the last command executed, but if you type up-arrow twice it gives you 'command a' because that was the command executed before 'command b'. If you want to recall 'command c' on Windows, you should type down-arrow.

    The Windows approach is probably superior because it makes it easier to recall a series of commands you executed some time ago, but it can be confusing and it is easy to trip up (especially when you switch between both systems).
  2. The way you search for previous commands:

    If you executed a command involving 'blog.txt' some time ago it might be tedious to have to keep typing up arrow to find it. Luckily both Windows and Linux provide a quick way to find the command in question.

    In Windows you would type the string 'blog.txt' and then hit the F8 key to see the last command containing that text. In Linux you first type CTRL-r and then start typing the text you want to match. As you are typing Linux will show you the command it would match for the text you have typed. The Linux way is clearly better because you can see how much text you need to type to match the command you want or you might decide to match on a different part of the command if you see lots of commands in your history containing the word 'blog.txt'.
  3. History persisting between sessions:

    In Linux your command history is stored in a file, which means that when you start a new session it automatically know your command history from a previous session. In contrast, Windows stores the command history in volatile memory so each new session starts with a clear slate, In general the Linux approach is much more useful, but there can be problems when you have two command windows open at the same time - the command history is effectively a combination of the commands typed in both windows which can be confusing.

3 comments:

  1. Useful post and reminder to shift your mind between win and linux! I didn't know powershell also implements bang (!) history substitution commands like bash. In that linux shell you can always run "set +o history" to avoid writing the current session commands into $HOME/.bash_history when you close the login session :-)
    Juan

    ReplyDelete
  2. Yes PowerShell is a very useful tool on Windows, but if you really want Linux type features you should install cygwin.

    ReplyDelete
  3. Thank you shares this article. I found this article more information of writing. You can try this Online Typing Services

    ReplyDelete