old-www/LDP/GNU-Linux-Tools-Summary/html/general-shell-tips.html

725 lines
13 KiB
HTML
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML
><HEAD
><TITLE
>General Shell Tips</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="GNU/Linux Command-Line Tools Summary"
HREF="index.html"><LINK
REL="UP"
TITLE="Shell Tips"
HREF="shell-tips.html"><LINK
REL="PREVIOUS"
TITLE="Shell Tips"
HREF="shell-tips.html"><LINK
REL="NEXT"
TITLE="The command-line history"
HREF="the-command-line-history.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>GNU/Linux Command-Line Tools Summary</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="shell-tips.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 4. Shell Tips</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="the-command-line-history.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="GENERAL-SHELL-TIPS"
></A
>4.1. General Shell Tips</H1
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
>Automatic Command Completion</DT
><DD
><P
>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.</P
><P
>There are also other lesser known ways to use automatic command completion (for example completing user names):<A
NAME="AEN1248"
HREF="#FTN.AEN1248"
><SPAN
CLASS="footnote"
>[1]</SPAN
></A
></P
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><B
CLASS="KEYCAP"
>ESC</B
>-<B
CLASS="KEYCAP"
>Y</B
>
 (Y: special character)</DT
><DD
><P
>testing autoindexing 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. </P
></DD
><DT
><B
CLASS="KEYCAP"
>CTRL</B
>-<B
CLASS="KEYCAP"
>X</B
>-<B
CLASS="KEYCAP"
>Y</B
>
 (Y: special character)</DT
><DD
><P
>Lists the possible completions (it won't attempt to complete it for you) or beep if there are no possible completions.</P
></DD
></DL
></DIV
><P
>Special-characters:</P
><P
>Use the following special characters combined with either
<B
CLASS="KEYCAP"
>ESC</B
>-<B
CLASS="KEYCAP"
>Y</B
>
or
<B
CLASS="KEYCAP"
>CTRL</B
>-<B
CLASS="KEYCAP"
>X</B
>-<B
CLASS="KEYCAP"
>Y</B
>
, where Y is some special characters. For example
<B
CLASS="KEYCAP"
>ESC</B
>-<B
CLASS="KEYCAP"
>$</B
>
or
<B
CLASS="KEYCAP"
>CTRL</B
>-<B
CLASS="KEYCAP"
>X</B
>-<B
CLASS="KEYCAP"
>$</B
>
to complete an environment variable name.</P
><P
></P
><UL
><LI
><P
>~ (tilde) complete a user name</P
></LI
><LI
><P
>@ (at sign) complete a machine name</P
></LI
><LI
><P
>$ (dollars sign) complete an environment variable name</P
></LI
><LI
><P
>! (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.</P
></LI
></UL
></DD
><DT
>alias</DT
><DD
><P
>The <EM
>alias</EM
> command will list your current aliases. You can use <EM
>unalias</EM
> to remove the alias (to disable it just for one command add a &#8220;\&#8221; (back-slash) before the command)...</P
><P
>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 <EM
>.bashrc</EM
> file (found in the users home directory).</P
><P
>Below is an example of what an alias section (within your <EM
>.bashrc</EM
> file) might look like:</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
># 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</PRE
></FONT
></TD
></TR
></TABLE
><P
>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.</P
></DD
><DT
>set -x</DT
><DD
><P
><EM
> set </EM
>is one of bash's inbuilt commands, try looking in the bash manual for its many usage options.</P
><P
>Using <EM
>set</EM
> with the<EM
> -x</EM
> option will make bash print out each command it is going to run before it runs it. </P
><P
>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<EM
> set +x</EM
> to turn this back off.</P
><P
>Examples</P
><P
>After using <EM
> set -x</EM
> you can run the command:</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>ls</PRE
></FONT
></TD
></TR
></TABLE
><P
>The output printed before the command runs (for example):</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>+ ls -F --color=auto </PRE
></FONT
></TD
></TR
></TABLE
><P
>Which means that the command is really an alias to run <EM
>ls</EM
> with the <EM
>-F</EM
> and <EM
>--color=auto</EM
> options. Use a &#8220;\&#8221; (backslash) before the command to run it without the alias.</P
></DD
><DT
>\ (backslash)</DT
><DD
><P
>The backslash escape character can be used before a shell command to override any aliases.</P
><P
>For example if <EM
>rm</EM
> was made into an alias for<EM
> rm -i</EM
> then typing &#8220;rm&#8221; would actually run<EM
> rm -i</EM
>. </P
><P
>However, typing <EM
>\rm</EM
> lets the shell ignore the alias and just run <EM
>rm</EM
> (its runs exactly what you type), this way it won't confirm if you want to delete things.</P
><DIV
CLASS="CAUTION"
><P
></P
><TABLE
CLASS="CAUTION"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="../images/caution.gif"
HSPACE="5"
ALT="Caution"></TD
><TH
ALIGN="LEFT"
VALIGN="CENTER"
><B
>Using rm</B
></TH
></TR
><TR
><TD
>&nbsp;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>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.</P
><P
>Only use <EM
>\rm</EM
> if you know exactly what you are doing (recovering files is not easy, <EM
>rm</EM
> does not send things to a recycle bin).</P
></TD
></TR
></TABLE
></DIV
><P
>The &#8220;\&#8221; 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 <EM
>cd</EM
> <EM
>My</EM
>\<EM
> Directory</EM
>\<EM
> With</EM
>\<EM
> Spaces</EM
> which normally wouldn't work. </P
><P
>The &#8220;\&#8221; 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).</P
><DIV
CLASS="TIP"
><P
></P
><TABLE
CLASS="TIP"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="../images/tip.gif"
HSPACE="5"
ALT="Tip"></TD
><TH
ALIGN="LEFT"
VALIGN="CENTER"
><B
>The TAB Key</B
></TH
></TR
><TR
><TD
>&nbsp;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>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).</P
></TD
></TR
></TABLE
></DIV
></DD
><DT
>script</DT
><DD
><P
>The &#8220;<EM
>script</EM
>&#8221; 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.</P
></DD
><DT
>~ (tilde character)</DT
><DD
><P
>The tilde character is used as an alias to a users home directory. </P
><P
>For example, if your user-name was &#8220;fred&#8221;, instead of typing<EM
> cd /home/fred </EM
>you could simply type<EM
> cd ~. </EM
>Or to get to fred's tmp directory (under his home directory) you could type<EM
> cd ~/tmp.</EM
></P
><DIV
CLASS="TIP"
><P
></P
><TABLE
CLASS="TIP"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="../images/tip.gif"
HSPACE="5"
ALT="Tip"></TD
><TH
ALIGN="LEFT"
VALIGN="CENTER"
><B
>Home directory shortcut</B
></TH
></TR
><TR
><TD
>&nbsp;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>~ (tilde) can also be used as a shortcut to other users home directories, simply type: <EM
>~user_name</EM
> and it will take you to the users home directory. Note that you need to spell the username exactly correct, no wildcards.</P
></TD
></TR
></TABLE
></DIV
></DD
><DT
></DT
><DD
><P
> </P
></DD
><DT
>set bell-style none</DT
><DD
><P
>This particular <EM
>set</EM
> 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 &#8220;.bashrc&#8221; or &#8220;.bash_profile&#8221; (just add it to the same one you have your alises in...).</P
></DD
><DT
>reset</DT
><DD
><P
>The <EM
>reset</EM
> command re-initializes your current terminal. This can be useful when the text from your terminal becomes garbled, simply type &#8220;reset&#8221; and this will fix your terminal.</P
></DD
><DT
>exit</DT
><DD
><P
>Closes your current terminal (with x-terminals) or logs-out. Also try
<B
CLASS="KEYCAP"
>CTRL</B
>-<B
CLASS="KEYCAP"
>D</B
>
.</P
></DD
><DT
>logout</DT
><DD
><P
>Logs out of a terminal, also try
<B
CLASS="KEYCAP"
>CTRL</B
>-<B
CLASS="KEYCAP"
>D</B
>
.</P
></DD
><DT
>echo</DT
><DD
><P
>A little command that repeats anything you type.</P
><P
>Example:</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>echo &#8220;hello world&#8221;</PRE
></FONT
></TD
></TR
></TABLE
><P
>Simply displays &#8220; hello world&#8221;. </P
><P
>Example:</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>echo rm -R *</PRE
></FONT
></TD
></TR
></TABLE
><P
>This will output what will be passed to the <EM
>rm</EM
> 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).</P
><P
>Also try using the<EM
> -e </EM
> 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.</P
><DIV
CLASS="TIP"
><P
></P
><TABLE
CLASS="TIP"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="../images/tip.gif"
HSPACE="5"
ALT="Tip"></TD
><TH
ALIGN="LEFT"
VALIGN="CENTER"
><B
>Using echo to prevent accidents</B
></TH
></TR
><TR
><TD
>&nbsp;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>Typing: <EM
>echo command(s) </EM
>could<EM
> </EM
>save you the trouble of accidentally doing something you didn't expect.</P
><P
>
Using <EM
>echo</EM
> allows you to expand the wildcards to understand what will happen before you actually run the command. </P
></TD
></TR
></TABLE
></DIV
></DD
></DL
></DIV
></DIV
><H3
CLASS="FOOTNOTES"
>Notes</H3
><TABLE
BORDER="0"
CLASS="FOOTNOTES"
WIDTH="100%"
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN1248"
HREF="general-shell-tips.html#AEN1248"
><SPAN
CLASS="footnote"
>[1]</SPAN
></A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>This information was adopted (with editing) from Mandrakesoft's Command Line Manual, see [7] in the <A
HREF="references.html"
><I
>Bibliography</I
></A
> for further information.</P
></TD
></TR
></TABLE
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="shell-tips.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="the-command-line-history.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Shell Tips</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="shell-tips.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>The command-line history</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>