Shell tips
The shell tips chapter provides handy tricks that you may wish to use when you are using a GNU/Linux shell (the command-line interface). This information includes handy shortcut key combinations, the shell's command history and information on virtual terminals.
If you can't boot into your system
If your having problems booting into your system you may like to use a shell so you can boot into your system and attempt to fix things up again.
To do this you need to pass the “init=/bin/sh” to your system before you boot up.
If you don't know how to do this please see Chapter 14, the technique is the same except this time you pass "init=bin/sh" rather than "single".
General Shell Tips
Automatic Command Completion
- Use the TAB key and bash will attempt to complete the command for you automatically. You can use it to complete command (tool) names. You can also use it when working with the file-system, when changing directories, copying files et cetera.
There are also other lesser known ways to use automatic command completion (for example completing user names):1
ESC-Y (Y: special character)
- Will attempt to complete the command name for you. If it fails it will either list the possible completions (if they exist). If there are none it will simply beep (and/or) flash the screen.
CTRL-X-Y (Y: special character)
- Lists the possible completions (it won't attempt to complete it for you) or beep if there are no possible completions.
- ~ (tilde) complete a user name
- @ (at sign) complete a machine name
- $ (dollars sign) complete an environment variable name
- ! (exclamation mark) a magic character for completing a command name or a file name. The ! special character has the same function as the TAB key. It works in some other situations; for example when completing man page names.
alias
- The alias command will list your current aliases. You can use unalias to remove the alias (to disable it just for one command add a “\” (back-slash) before the command)... An alias allows one command to be substituted for another. This is used to make a command do something else or to automatically add certain options. This can be either be done during one session using the alias command (see below) or the information can be added to the .bashrc file (found in the users home directory). Below is an example of what an alias section (within your .bashrc file) might look like:
# my personal aliases alias cp='cp -vi' #to prompt when copying if you want to overwrite and will tell you where information is going alias rm='rm -i' #Prompts you if you really want to remove it. alias mv='mv -i' #Prompts you if you are going to overwrite something
On any Mandriva GNU/Linux system the global aliases (for all users) are all in /etc/profile.d/alias.sh. The above listed commands already have aliases, as well as several other commonly used commands.
set -x
- set is one of bash's inbuilt commands, try looking in the bash manual for its many usage options. Using set with the -x option will make bash print out each command it is going to run before it runs it. This can be useful to find out what is happening with certain commands such as things being quoted that contain wildcards or special symbols that could cause problems, or complex aliases. Use set +x to turn this back off. Examples After using set -x you can run the command:
ls
The output printed before the command runs (for example):+ ls -F --color=auto
Which means that the command is really an alias to run ls with the -F and --color=auto options. Use a “\” (backslash) before the command to run it without the alias.
\ (backslash)
- The backslash escape character can be used before a shell command to override any aliases. For example if rm was made into an alias for rm -i then typing “rm” would actually run rm -i. However, typing \rm lets the shell ignore the alias and just run rm (its runs exactly what you type), this way it won't confirm if you want to delete things. The “\” character can be used before special characters (such as a space or a wildcard), to stop bash from trying to expand them. You can make a directory name with a space in it using a backslash before the space. For example you could type cd My\ Directory\ With\ Spaces which normally wouldn't work. The “\” character can also be used to stop bash from expanding certain symbols (as an alternative you could use single quotation marks, although you may need to use both).
Using rm
Please note that the alias for the remove command is there for a reason. Using it incorrectly could remove files which you don't want removed. Only use \rm if you know exactly what you are doing (recovering files is not easy, rm does not send things to a recycle bin).
The TAB Key
Please note that using the TAB key (automatic-command-completion) will automatically use escapes for spaces (so you don't have to type them manually).
script
- The “script” command creates a typescript, or "capture log" of a shell session - it writes a copy of your session to a file, including commands you type and their output.
~ (tilde character)
- The tilde character is used as an alias to a users home directory. For example, if your user-name was “fred”, instead of typing cd /home/fred you could simply type cd ~. Or to get to fred's tmp directory (under his home directory) you could type cd ~/tmp.
Home directory shortcut
~ (tilde) can also be used as a shortcut to other users home directories, simply type: ~user_name and it will take you to the users home directory. Note that you need to spell the username exactly correct, no wildcards.
set bell-style none
- This particular set command will turn off the system bell from the command-line (use xset -b for X windows). If you want the bell to stay off pernamently (no audible bell) then you can add this command to your “.bashrc” or “.bash_profile” (just add it to the same one you have your alises in...).
reset
- The reset command re-initializes your current terminal. This can be useful when the text from your terminal becomes garbled, simply type “reset” and this will fix your terminal.
exit
Closes your current terminal (with x-terminals) or logs-out. Also try CTRL-D.
logout
Logs out of a terminal, also try CTRL-D.
echo
- A little command that repeats anything you type. Example:
echo “hello world”
Simply displays “ hello world”. Example:echo rm -R *
This will output what will be passed to the rm command (and therefore what would be deleted), putting echo before a command renders it harmless (it just expands wildcards so you know what it will do). Also try using the -e option with echo. This will allow you to use the escape character sequences to format the output of a line. Such as '\t' for tab, '\n' for newline etc.Using echo to prevent accidents
Typing: echo command(s) could save you the trouble of accidentally doing something you didn't expect.
Using echo allows you to expand the wildcards to understand what will happen before you actually run the command.
The command-line history
Using the command history
- Use the up and down key's to scroll through previously typed commands. Press [Enter] to execute them or use the left and right arrow keys to edit the command first. Also see history (below).
The history command
- The history command can be used to list Bash's log of the commands you have typed: This log is called the “history”. To access it type:
history n
This will only list the last n commands. Type “history” (without options) to see the the entire history list. You can also type !n to execute command number n. Use !! to execute the last command you typed. !-n will execute the command n times before (in other words !-1 is equivalent to !!). !string will execute the last command starting with that “string” and !?string? will execute the last command containing the word “string”. For example:!cd
Will re-run the command that you last typed starting with “cd”. “ commandName !*” will execute the “commandName” with any arguments you used on your last command. This maybe useful if you make a spelling mistake, for example. If you typed:emasc /home/fred/mywork.java /tmp/testme.java
In an attempt to execute emacs on the above two files this will obviously fail. So what you can do is type:emacs !*
This will execute emacs with the arguments that you last typed on the command-line. In other words this is equivalent to typing:emacs /home/fred/mywork.java /tmp/testme.java
Searching through the Command History ( CTRL-R )
Use the CTRL-R key to perform a “reverse-i-search”. For example, if you wanted to use the command you used the last time you used snort, you would type:
CTRL-R then type “snort”. What you will see in the console window is:
(reverse-i-search)`':
After you have typed what you are looking for, use the CTRL-R key combination to scroll backward through the history.
Use CTRL-R repeatedly to find every reference to the string you've entered. Once you've found the command you're looking for, use [Enter] to execute it. Alternatively, using the right or left arrow keys will place the command on an actual command-line so you can edit it.
Other Key combinations
GNU/Linux shells have many shortcut keys which you can use to speed up your work, below is a rough list of some (also see CTRL-R in the history section of the commands, over here, Section 4.2).
CTRL-D
the “end-of-file” (EOF) key combination can be used to quickly log out of any terminal. CTRL-D is also used in programs such as “at” to signal that you have finished typing your commands (the EOF command).
CTRL-Z
- key combination is used to stop a process. It can be used to put something in the background temporarily.
For example, if you were editing a file with vim or emacs just press CTRL-Z to regain control of the terminal do what you want and then type fg to bring it back. For further information please see Section 9.3.
If fg doesn't work
If fg doesn't work you may need to type jobs and then fg job_name or fg job_number
CTRL-A and CTRL-E
These key combinations are used for going to the start and end of the line on the command line. Use CTRL-A to jump to the start of the line, and CTRL-E to jump to the end of the line.
CTRL-K
- This key combination can be used to cut or delete what is currently in front of the cursor.
CTRL-Y
This key combination can be used to paste the last thing you deleted (using CTRL-K or CTRL-W ).
CTRL-W
- This key combination can be used to cut or delete the entire line that has being typed.
Virtual Terminals and screen
Using the key combination ALT-F* keys you may change to different virtual terminals. You will have several (usually 6) virtual terminals setup with shells. Number 7 is usually setup with X you need to use CTRL-ALT-F* to change to a terminal from within X (X as in the X windowing system).
screen
- is a great program that allows you to switch between multiple virtual terminals on the one physical terminal that you are using. Its a command-line based window manager, clearly this isn't that useful if you do have virtual terminals, but its amazingly useful when you log into machines remotely, using ssh and similar, see Section 13.3. It works on key-combinations, you type
screen
On the command-line to begin. Now you start with one virtual terminal by default, but using the key combination CTRL-A and then hitting "C" you can create another virtual terminal to use.
Use CTRL-N to go to the next virtual terminal and CTRL-P to go to the previous virtual terminal. Also try hitting CTRL-A to go backwards and forwards between two particular terminals. screen also has various other abilities that you can test out. The documentation and guides are well written so please feel free to read the manual page or try searching the internet.
This information was adopted (with editing) from Mandrakesoft's Command Line Manual, see [7] in the Bibliography for further information. (1)