Shell Introduction and basic usage

Source: Internet
Author: User
Tags aliases egrep


8.1:shell Introduction:

The shell is a command interpreter that provides interaction between the user and the machine, supports specific syntax, supports logical judgments, loops, and each user can have his or her own shell:

The CentOS default shell is bash (Bourne Agin shell): In fact, to commemorate the creator of SH Bourne this user:

There are also common zsh (Power-shell) Ksh (Korn-shell): The supported features are relatively small:

8.2: The Command history: Historical

Commands that users use in the system are saved and saved in the current user's home directory:

History Command

Syntax: history [-c]
-c:=clear clears the in-memory command and cannot delete the history command in the configuration file

[[email  Protected] ~]# history 
   1  ls
   2  ls/tmp/
   3  LS/BOOT/
   4  ls/
   5  dhclient
...
[[email protected] ~]# ls/root/.bash_history/root/.bash_history     history's home directory

Show used command history, save 1000 used commands by default (note: This order needs to be in a normal shutdown operation in the case of 1000 life)!

History Environment Variables
    • Variable histsize

[Email protected] ~]# echo $HISTSIZE 1000

This variable determines the number of commands saved by the command history.

    • Defining variables Histsize

 edit its configuration file 
[email  protected] ~]# vim/etc/profile ...
Hostname= '/usr/bin/hostname 2>/dev/null '
histsize=1000 ...
[[email protected] ~]# echo $HISTSIZE 1000[[email protected] ~]# source/etc/profile[[email  Protected] ~]# echo $HISTSIZE

Search the keyword "HIST" to find ' histsize=1000 ', change its number here, save exit, and then execute the command ' source/etc/profile ' to refresh the profile before it takes effect.

    • Change the history display format

[[email  Protected] ~]# echo $HISTTIMEFORMAT [[email protected] ~]# histtimeformat= "%y/%m/%d%h:%m:%s" [[email  Protected] ~]# echo $HISTTIMEFORMAT%y/%m/%d%h:%m:%s 
[[email protected] ~]# history
   1   2017/06/28 18:50:11 history    2  2017/06/28 18:51:32 echo $HISTTIMEFORMAT    3  2017/06/28 18:51:43 histtimeformat= "%y/%m/%d%h:%m:%s"
   4  2017/06/28 18:51:45 echo $HISTTIMEFORMAT   &NB Sp;5  2017/06/28 18:52:32 History

Assign a value directly to ' Histtimeformat ', but this format only applies to the current terminal. If you want to use it for all users, you need to write it to the history profile and take effect after the refresh.

[Email protected] ~]# vim/etc/profile ...
Hostname= '/usr/bin/hostname 2>/dev/null ' histsize=1000histtimeformat= "%y/%m/%d%H:%M:%S" ...
Save Exit!
[Email protected] ~]# Source/etc/profile
    • Command history Save Forever
      That is, make the command history write-only cannot be deleted!

[Email protected] ~]# chattr +a ~/.bash_history

Use file special permissions to configure ' A ' permissions for '. Bash_history ' files (can only be appended, not deleted), limited to normal shutdown operations.

'! ' Command
[Email protected] ~]# W ...
[[email protected] ~]#!! W
......

"! ' Usage: '! N ' (n for number) indicates the nth command in the Run command history; '! Word ', which represents the last command to run with this word.
Eg:

[Email protected] ~]# history
1 2017/06/28 18:50:11 history 2 2017/06/28 18:51:32 echo $HISTTIMEFORMAT 3 2017/06/28 18:51:43 histtimeformat= "%Y /%m/%d%h:%m:%s "
4 2017/06/28 18:51:45 echo $HISTTIMEFORMAT 5 2017/06/28 18:52:32 History
[Email protected] ~]#!4echo $HISTTIMEFORMAT
%y/%m/%d%h:%m:%s
[[email protected] ~]#! histhistsize=1000
8.3 command completion and aliasesCommand Completion tab

Press TAB to complete a command or parameter (need to install package Bash-completion, restart the system); Press two tab to display all commands or filenames that begin with a letter.

alias Command

Syntax: alias [command alias]=[specific command] set aliases
Alias: Unalias [Command alias]

Direct input alias displays all aliases for the system:

[[email  Protected] ~]# aliasalias cp= ' cp-i ' Alias egrep= ' Egrep--color=auto ' Alias fgrep= ' Fgrep--color=auto ' Alias grep= ' grep-- Color=auto ' Alias l.= ' ls-d. *--color=auto ' Alias ll= ' Ls-l--color=auto ' Alias ls= ' ls--color=auto ' alias mv= ' Mv-i ' Alias RM = ' rm-i ' Alias Which= ' Alias | /usr/bin/which--tty-only--read-alias--show-dot--show-tilde ' [[email protected] ~]# 

system aliases are stored in config file ' ~/.bashrc ' and ' ls/etc/ Profile.d/' Down:

[email protected] ~]# cat! $cat. bashrc#. bashrc# User specific aliases and Functionsalias rm= ' rm-i ' Alias cp= ' Cp-i ' alia S mv= ' mv-i ' # Source global definitionsif [-F/ETC/BASHRC]; Then
. /etc/bashrcfi[[email protected] ~]# ls/etc/profile.d/256term.csh colorgrep.sh lang.sh qt-graphics System.sh which2.sh
256term.sh colorls.csh less.csh vim.csh
bash_completion.sh colorls.sh less.sh vim.sh
COLORGREP.CSH lang.csh qt-graphicssystem.csh which2.csh
8.4 Wildcard Characters
    • The wildcard ' * ' stands for 0 or more arbitrary characters

    • Wildcard characters '? ' Represents an arbitrary character

    • Brackets ' [] ', ' ls [0-9].txt ' means any. txt file within the 0-9 interval

    • Curly braces ' {} ', ' ls {1,2,3}.txt ' means any. txt file in parentheses

input and output redirection

" >,>>,<,2> ,2>> "
' > ': Output redirection
' >> ': Append redirect
' 2> ': Error redirection
' < ': input redirection
using ' > ' The original content in the file is deleted when the command is

[[email protected] tmp]# echo adaixuelinux > 1.txt[ [email protected] tmp]# cat 1.txtadaixuelinux 
[[email protected] tmp]# echo Adaixu > 1.txt[[email  protected] tmp]# cat 1.txtadaixu#####################################[[email protected] tmp]# echo Adaixu >> 1.txt[[email protected] tmp]# cat 1.txtadaixu
Adaixu#####################################[[email  protected] tmp]# lsaaa-bash:lsaaa: Command not found
[[email protected] tmp]# lsaaa 2> 2.txt[[email  Protected] tmp]# cat 2.txt-bash:lsaaa: Command not found

Input redirection: Must be directed to (< left) under one command
[[email protected] tmp]# wc-l 1. TXT   "wc-l" This command is used to view the number of file lines 2 1.txt
    • application

[[email  Protected] tmp]# ls {1,2}.txt aaaa.txt > 1.txt 2> 3.txt[[email protected] tmp]# cat 1.txt1.txt2.txt 
[[Email  protected] tmp]# cat 3.TXTLS: Unable to access aaaa.txt: No file or directory

Description: use the LS command to view {1,2}.txt aaaa.txt,1.txt and 2.txt files exist, can use LS to view, Aaaa.txt does not exist, using LS view will error, ' > 1.txt 2> 3. TXT ' means to save the correct information to 1.txt and save the error message to 3.txt.



Shell Introduction and basic usage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.