Outils pour utilisateurs

Outils du site


issue102:encart

Using BASH command history by Jeremy Boden Would you like the command entry screen to be a bit less work to use? Instead of repetitive typing of commands, access to previously entered commands can be a big help. This feature works either in a “real” command entry screen (accessed via one of the CTRL+ALT+F1 thru CTRL+ALT+F6 shortcuts) or perhaps more commonly, via the Terminal window option. It turns out that a command entry screen keeps a copy of (typically) the last 500 commands entered. One option would be to enter the history command (without any parameters), I (currently) see a long list:­ [493 lines omitted] 494 locate amstex.sty 495 cd … 500 sudo apt­get clean 501 sudo du ­h /backup | sort ­k 2 502 history

Utiliser la commande BASH history Par Jeremy Boden

Voudriez-vous que l'écran de saisie des commandes soit un peu moins fastidieux à utiliser ? Au lieu de répéter la saisie des commandes, l'accès aux commandes entrées précédemment peut vous être d'une grande aide. Cette fonctionnalité marche, soit dans un des écrans d'entrée de commande « réels » (accessibles via un des raccourcis CTRL+ALT+F1 à CTRL+ALT+F6), soit, et peut-être plus communément, par l'option de la fenêtre de Terminal.

Il se trouve qu'un écran d'entrée de commande garde une copie de (typiquement) les 500 dernières commandes entrées. Une option serait de saisir la commande history (sans aucun paramètre) ; ainsi, je vois (actuellement) une longue liste :

[493 lignes omises] 494 locate amstex.sty 495 cd … 500 sudo apt­get clean 501 sudo du ­h /backup | sort ­k 2 502 history

Note that the commands entered are listed in reverse order, so that the most recent command is shown last. Actually, the history command comes with a multitude of options – which I shan't discuss. Instead, we can access the command history in a simple interactive way. It is a “well known fact” that pressing the up­arrow/down­arrow keys will scroll through your command history; in particular the up­arrow will display the previous command in a command screen, clearly you will want to avoid pressing this key too many times! So we search our history using “reverse intelligent search”. My command screen is waiting for me to enter a command, so it reads:­ jeremy@hector:~$ Pressing CTRL+R, will cause this to change to:­ (reverse­i­search)`': I entered mlo – (I was expecting to type mlocate) and my screen changed to:­ (reverse­i­search)`mlo': sudo /usr/bin/updatedb.mlocate

Notez que les commandes saisies sont listées dans l'ordre inverse, de sorte que les dernières entrées sont affichées en dernier. En réalité, la commande history dispose d'une multitude d'options, dont je ne parlerai pas. À la place, nous pouvons accéder à la commande history de manière simple et interactive.

C'est « un fait bien connu » que l'appui sur les boutons fléchés haut/bas permet de faire défiler l'historique des commandes ; en particulier, la flèche vers le haut affichera la commande précédente à l'écran ; clairement, vous ne voulez pas presser cette touche trop de fois ! Aussi, nous cherchons notre historique en utilisant « la recherche intelligente inverse ».

Mon écran de commande attend la saisie d'une commande ; je peux donc lire : jeremy@hector:~$

L'appui sur CTRL+R entraînera un changement en : (reverse­i­search)`':

J'entre mlo (je souhaite taper mlocate) et mon écran change en : (reverse­i­search)`mlo': sudo /usr/bin/updatedb.mlocate

• Note that the search string is shown enclosed between a backtick character and a single quote and separated from the full command by a colon. I chose to run this command unchanged, by pressing the enter key. • If your search string includes spaces, enter the exact number of spaces required. • Note that in my search, it was only necessary to enter a few adjacent characters – it is not necessary to start from the beginning of the command. • It may happen that your search string matches a number of different commands – to access an older command, just press CTRL+R again. • To amend and run the retrieved command, press the left­arrow or right­arrow keys, type in the alterations and press enter to run the command. • At any point, prior to pressing the enter key, you can abandon running the command by pressing CTRL+C. After pressing enter (to run the command) or CTRL+C to cancel any command execution, your command entry screen will return to its original appearance.

Notez que la chaîne de caractères à rechercher est entre une apostrophe inverse et un guillemet simple et séparé de la commande entière par un deux-points. Je choisis de lancer la commande telle quelle, en appuyant sur la touche Entrée. • Si votre chaîne comprend des espaces, entrez le nombre exact d'espaces nécessaires. • Notez que dans ma recherche, il a suffi de quelques caractères contigus - il n'est pas nécessaire de commencer par le début de la commande. • Il pourrait arriver que votre chaîne à rechercher corresponde à plusieurs commandes ; pour atteindre une commande plus ancienne, tapez simplement CTRL+R à nouveau. • Pour modifier la commande retrouvée avant de la lancer, appuyez sur la touche fléchée droite ou gauche, tapez les modifications et appuyez sur Entrée pour lancer la commande. • À tout moment, avant d'appuyer sur Entrée, vous pouvez renoncer à lancer la commande en appuyant sur CTRL+C.

Après l'appui sur Entrée (pour lancer la commande), ou CTRL+C pour annuler toute exécution de cette commande, l'écran de saisie des commandes revient à son aspect initial.

issue102/encart.txt · Dernière modification : 2015/11/13 16:22 de andre_domenech