Difference between revisions of "Bash"

From Briki
Jump to: navigation, search
(Force autologout of root and other privileged account shells after inactivity)
Line 1: Line 1:
 +
== History Manipulation ==
 +
 +
To repeat a previous command or subsection of a previous command, optionally performing some kind of modification to it, run ''event''[''':'''''word''][''':'''''modifier''][''':'''''modifier'']....
 +
 +
=== Event Designators ===
 +
{| border="1"
 +
!Designator!!Meaning
 +
|-
 +
|'''!!'''||The previous command.
 +
|-
 +
|'''!n'''||Command number '''n''' in the history list.
 +
|-
 +
|'''!-n'''||The '''n'''th preceding command.
 +
|-
 +
|'''!string'''||The most recent command line that started with '''string'''.
 +
|-
 +
|'''!?string[?]'''||The most recent command that contained '''string'''. The last '''?''' is optional.
 +
|-
 +
|'''!#'''||The current command (as you have it typed so far).
 +
|-
 +
|'''!{event}'''||The event is an event designator. The braces isolate event from the surrounding text. For example, '''!{-3}3''' is the third most recently executed command followed by a 3.
 +
|}
 +
 +
=== Word Designators ===
 +
{| border="1"
 +
!Designator!!Meaning
 +
|-
 +
|'''n'''||The '''n'''th word. Word 0 is normally the command name.
 +
|-
 +
|'''^'''||The first word (after the command name).
 +
|-
 +
|'''$'''||The last word.
 +
|-
 +
|'''m-n'''||All words from word number '''m''' through word number '''n'''; '''m''' defaults to 0 if you omit it (0-n).
 +
|-
 +
|'''n*'''||All words from word number '''n''' through the last word.
 +
|-
 +
|'''*'''||All words except the command name. The same as 1*.
 +
|-
 +
|'''%'''||The word matched by the most recent '''?string?''' search.
 +
|}
 +
 +
=== Modifiers ===
 +
 +
{| border="1"
 +
!Designator!!Meaning
 +
|-
 +
|'''e''' (extension)||Removes all but the filename extension
 +
|-
 +
|'''h''' (head)||Removes the last part of a pathname
 +
|-
 +
|'''p''' (print-not)||Displays the command, but does not execute it
 +
|-
 +
|'''q''' (quote)||Quotes the substitution to prevent further substitutions on it
 +
|-
 +
|'''r''' (root)||Removes the filename extension
 +
|-
 +
|'''[g]s!old!new[!]''' (substitute)||Substitutes '''new''' for '''old''' (eg. '''!!:0:s!ch!lm''' runs the first word of the previous command, substituting '''lm''' for '''ch''' in the string). The optional '''g''' replaces all occurrences of '''old''' rather than just the first one. The trailing '''!''' is optional if the command is immediately followed by a RETURN.
 +
|-
 +
|'''t''' (tail)||Removes all elements of a pathname except the last
 +
|-
 +
|'''x'''||Like q but quotes each word in the substitution individually
 +
|}
 +
 
== Force autologout of root and other privileged account shells after inactivity ==
 
== Force autologout of root and other privileged account shells after inactivity ==
 
Add to /etc/bash.bashrc (not /etc/profile, or it will only be set when running '''su -''' and not '''su'''):<pre>
 
Add to /etc/bash.bashrc (not /etc/profile, or it will only be set when running '''su -''' and not '''su'''):<pre>

Revision as of 16:11, 2 November 2006

History Manipulation

To repeat a previous command or subsection of a previous command, optionally performing some kind of modification to it, run event[:word][:modifier][:modifier]....

Event Designators

Designator Meaning
!! The previous command.
!n Command number n in the history list.
!-n The nth preceding command.
!string The most recent command line that started with string.
!?string[?] The most recent command that contained string. The last ? is optional.
!# The current command (as you have it typed so far).
!{event} The event is an event designator. The braces isolate event from the surrounding text. For example, !{-3}3 is the third most recently executed command followed by a 3.

Word Designators

Designator Meaning
n The nth word. Word 0 is normally the command name.
^ The first word (after the command name).
$ The last word.
m-n All words from word number m through word number n; m defaults to 0 if you omit it (0-n).
n* All words from word number n through the last word.
* All words except the command name. The same as 1*.
% The word matched by the most recent ?string? search.

Modifiers

Designator Meaning
e (extension) Removes all but the filename extension
h (head) Removes the last part of a pathname
p (print-not) Displays the command, but does not execute it
q (quote) Quotes the substitution to prevent further substitutions on it
r (root) Removes the filename extension
[g]s!old!new[!] (substitute) Substitutes new for old (eg. !!:0:s!ch!lm runs the first word of the previous command, substituting lm for ch in the string). The optional g replaces all occurrences of old rather than just the first one. The trailing ! is optional if the command is immediately followed by a RETURN.
t (tail) Removes all elements of a pathname except the last
x Like q but quotes each word in the substitution individually

Force autologout of root and other privileged account shells after inactivity

Add to /etc/bash.bashrc (not /etc/profile, or it will only be set when running su - and not su):
if [ `id -u` -lt 500 ]; then
    # Override console timeout for root and other system users
    TMOUT_ROOT=3600
    if [ -n "$TMOUT" ]; then
        if [ "$TMOUT" -eq "0" -o "$TMOUT" -gt "$TMOUT_ROOT" ]; then
            TMOUT=$TMOUT_ROOT
        fi
    else
        TMOUT=$TMOUT_ROOT
    fi
fi
type declare > /dev/null 2>&1 && declare -r TMOUT

You can probably get rid of the declare line at the bottom - this just ensures that TMOUT cannot subsequently be modified.