Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

4.1. 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.

Special-characters:

Use the following special characters combined with either ESC-Y or CTRL-X-Y , where Y is some special character. For example ESC-$ or CTRL-X-$ to complete an environment variable name.

  • ~ (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 Mandrake/Mandriva 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.

Caution 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 "\" character can be used before special characters (such as a space or a wildcard...) to stop bash from trying to expand them (a directory name with a space in it is possible this way). 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).

Tip 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.

Tip 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 user's 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 commandline (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's text 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.

Tip 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.

Notes

[1]

This information was adopted (with editing) from Mandrakesoft's Command Line Manual, see [7] in the Bibliography for further information.

 
 
  Published under the terms of the GNU General Public License Design by Interspire