Linux system study notes: shell environment

Source: Internet
Author: User
Linux system study notes: the shell environment Linux system contains a powerful shell environment, as a command interpreter. Through the shell environment, you can not only interact with a large number of command line tools, but also support script programming to complete more complex and time-consuming work in batches. BASH is the object... Linux system learning notes: shell environment Linux system contains a powerful shell environment, as a command interpreter. Through the shell environment, you can not only interact with a large number of command line tools, but also support script programming to complete more complex and time-consuming work in batches. BASH is the default shell interpreter for most Linux versions. This article summarizes the shell environment based on BASH. In the terminal simulation window of the text interface or graphic interface, shell runs as a command interpreter, which is called a shell environment. There are multiple versions of shell, usually BASH is used more. BASH performs global configuration through the/etc/profile file. When logging on, it will find and execute ~ /. Bash_profile ,~ /. Bash_login ,~ /. Profile, executed at logout ~ /. Bash_logout. The/etc/bashrc and ~ commands will be executed during non-logon ~ /. Bashrc file. Non-interactive BASH does not execute the commands in these initialization files, only inherits the variables they set, and executes the commands in the file specified by the BASH_ENV environment variable. When you need to modify the default environment variables (~ /. Bash_profile ,~ /. Profile,/etc/profile. Commands such as command alias in rc file (~ /. Bashrc,/etc/bash. bashrc. The shell command is executed after you press enter, and can be modified before this. The following are the functions of some keys in the shell environment: backspace Delete ^ H Delete character ^ W Delete word ^ U Delete row ^ C interrupt ^ Z suspend ^ D exit current user login shell the input command syntax is: cmd [arg]... [arg] generally starts with the command name, followed by optional parameters. Parameters starting with "-" or "--" are also called options. they are usually placed before common parameters and are interpreted by commands. the options starting with "-" can be merged. You can use -- to indicate that the following parameters are not interpreted as options. Shell searches for programs with execution permissions under the PATH specified by the PATH environment variable. Special characters in shell include :'! # $ % ^ & * + ~ \ | ;'"/? <> () [] {} Can be escaped by adding \ before special characters, or you can enclose them by using single quotes. You can press ^ V to enter the control character.; Can separate multiple commands, but do not execute them immediately. If a single command is too long, you can use \ to switch to the next line to continue the input. () You can group commands. shell creates a sub-shell for each group of commands and uses them as a job. # Start line comment. & | It is logical and logical, or the connection command can be used for short-circuit judgment.! Returns the exit status of the command. In addition, execute the command at the beginning of the line. The process that redirects a program opens three file descriptors: standard input (0), standard output (1), and standard error output (2 ). Generally, the standard input is the keyboard, and the standard output and the standard error output are shell output. you can change the association between the standard input and output through redirection. <Redirect standard input (abbreviated as 0 <),> redirect standard output (abbreviated as 1>), 2> redirect standard error output,> append standard output to a file, <redirect the script content to the script input. <The redirection content is referred to as the Here document. one or more symbols are used to define the content. the start of the definition must be next to <. the end of the definition must be an exclusive line. Redirection will overwrite existing files. you can set noclobber to disable rewrite during redirection, while> | can ignore noclobber settings. /Dev/null is called a bit bucket. you can redirect the standard output to/dev/null to discard the output content. $ Set-o noclobber $ date -- rfc-3339 = seconds> d.txt $ cat d.txt 2012-03-08 16:55:46 + 08:00 $ date -- rfc-3339 = seconds> d.txt-bash: d.txt: cannot overwrite existing file $ date -- rfc-3339 = seconds> | d.txt $ cat d.txt 2012-03-08 16:56:00 + 08:00 $ set + o noclobber $ date -- rfc-3339 = seconds> d.txt $ cat d.txt 2012-03-08 16:56:31 + 08:00 $ date -- rfc-3339 = seconds> d.txt $ cat d.txt 16:56:31 + 08:002012-03-0 8 16:57:14 + $ cat/dev/null> d.txt # DELETE file content $ grep-I alex <+> Alex> Harry> Nancy> + Alex can be redirected file name, n is the file descriptor. Redirection to &-indicates that the descriptor is disabled. $ Cat afile a $ cat bcat: B: No such file or directory $ cat a B> out # redirect standard output to outcat: B: no such file or directory $ cat outfile a $ cat a B 1> out 2> & 1 # redirect standard output to out, redirect standard error output to standard output (that is, out) $ cat outfile acat: B: No such file or directory $ cat a B 2> & 1 1> out # redirect standard error output to standard output, redirect standard output to outcat: B: no such file or directory $ cat outfile a pipe in shell environment can use the output of the previous command as the input of the next command. the pipeline does not actually process each command separately, and no intermediate files are generated. Syntax: cmd [args] | cmd [args] pipelines can be connected in series or in combination with redirection. $ Who | tee w. out | grep yeolar # w. out File yeolar pts/2 2012-03-08 (yeolar-pc.local) tee command can copy standard input to standard output and file. By default, commands run in the background on the foreground. after the command is executed, the control is handed back to shell. You can run the command in the background without waiting for the command to be executed. The command sequence for connecting commands or pipelines is called a job. multiple jobs can be run in the background. Running multiple jobs at the same time is called a multitasking feature. Add & at the end of the command line to run the job in the background. Shell assigns a job number to the job. Jobs displays the job list. You can use the fg command (or %) to move the background job to the foreground, and use the bg command to move the suspended job to the background for running. $ Cat & # run cat in the background, job number 1, PID 2640; suspend sleep, job number 2 [1] 2640 $ sleep 10 ^ Z [1]-Stopped cat [2] + Stopped sleep 10 $ date -- rfc-3339 = s2012-03-09 09:07:17 + 08:00 $ date -- rfc-3339 = s # wait until 09:07:32 + $ bg # The sleep job is still in progress, move to the background and run [2] + sleep 10 & $ jobs # to display the job list, sleep ends [1] + Stopped cat [2]-Done sleep 10 $ % 1 # Move Job 1 to the foreground due to timeout; suspend cat, job No. 1cat ^ Z [1] + Stopped cat $ bg 1 # Move Job 1 to the background [1] + cat & $ jobs # display job list [1] + Stopp Ed cat $ fg # Move Job 1 to the foreground; interrupt catcat ^ C directory stack dirs command to display directory stack, pushd change directory, and press directory into directory stack, if no parameter is added, the two directories at the top of the stack are switched. popd pops up the directory from the stack. If no parameter is added, the directory at the top of the stack is displayed and changed to the directory. $ Mkdir-p a/B/c $ dirs ~ $ Pushd /~ /~ $ Pushd B/c/# pwd :~ /~ /A/B/c ~ /~ $ Pushd # pwd :~ /A/B/c ~ /~ /A/B/c ~ $ Pushd B/# pwd :~ /~ /A/B ~ /~ /A/B/c ~ $ Pushd + 1 # pwd :~ /A/B ~ /~ /A/B/c ~ ~ /A/B $ popd + 1 # pwd :~ /~ /~ ~ /A/B $ popd # pwd :~ /~ ~ /A/B $ popd + 1 ~ Command History HISTSIZE, HISTFILE, and HISTFILESIZE can be used to set the number of historical commands to be saved and the number of files to be saved. You can view (-l), edit (no parameter or-e), and re-execute (-s) the fc command. You can also use it! Reference event (that is, command ):!! Run the previous command! N: run the command whose history list number is n. If n is negative, run the nearest command | n |! Str: execute the latest command starting with str !? Str [?] Execute the command that recently contains str, followed? Yes! # The part already entered in the current command! The {event }{} command separates the event from the left and right texts. you can use the symbol and modifier to modify and replace the event. The command line editing of the BASH in the Readline library is implemented through the Readline library. There are two editing modes available: vi and emacs. Commands such as command line completion and some shortcut keys are derived from the Readline library. Another convenient function of alias BASH is alias, which simplifies the command through alias. Alias can be nested. It is usually configured in the BASH rc configuration file. $ Aliasalias ls = 'ls -- color = Auto' $ alias ll = 'ls-L' to extend {}, the separated strings are extended to the string list separated by space characters. the {} can have optional prefixes and suffixes, which are added to both ends of each string in the string list. $ Touch a0000,, seconds .txt $ lsa1.txt a2.txt a3.txt a4.txt a5.txt ~ Extended ~ Replace the value of the HOME variable. If ~ The user name is followed by a valid user name, which is replaced with the user's home directory. In the directory stack ,~ + Is the current working directory ,~ -Is the previous working directory. $ Echo ~ Root/root arithmetic extension $ (expr) calculates expr and replaces it with the result. BASH uses integers for calculation. In $ (), the variable quote $ can be omitted. The let command also calculates the value of the arithmetic expression and can pass multiple parameters to it. $ X = 3 y = 7; echo $ (x + y)/2) 5 $ let m = (x + y)/2 n = (x + y) /m; the echo $ m $ n5 2 command replaces the syntax of the command with two types: $ (cmd) and 'cmd'. they replace the command with the command output. $ Ls-l $ (find. -name README-print) for file names that contain special characters, shell can generate a file name that matches the name of an existing file. this is called PATH extension or wildcard. special characters are called wildcards.? Match a single character. * matches 0 or multiple characters. [] matches any character in square brackets. you can use-to specify the character range or use it before the character list! Or ^ indicates that it does not match any character. $ Touch a111 a1 a2 a3 a5 b2 b3 c5 $ echo * a1 a111 a2 a3 a5 b2 b3 c5 $ echo. *... $ echo a * a1 a111 a2 a3 a5 $ echo? A1 a2 a3 a5 $ echo a [1-3] a1 a2 a3 $ echo [! 1-3] a5 $ echo a [^ 1-3] a5 $ echo [a-z] * a1 a111 a2 a3 a5 b2 b3 c5 $ echo [a-z]? The shell variable of a1 a2 a3 a5 b2 b3 c5 is composed of letters (including _) and numbers and starts with a letter. Global variables, also known as environment variables, are usually capitalized. Use the = sign to create a variable. The variable value is saved as a string by default. Add $ Before the variable name to reference the variable. you can add {} on both sides of the variable name to eliminate ambiguity. Value assignment statement = if the right side is empty, the value can be left blank. the unset command deletes the variable. Single quotation marks and double quotation marks interpret the content as strings, and single quotation marks prevent reference. $ Fa = a * $ echo fafa $ echo $ faa1 a2 a3 $ echo $ {fa} aa * a $ echo "$ fa" a * $ echo '$ fa can use declare or typeset to set variable attributes, -a declares the array variable-f declares the function variable-I declares the integer variable-r declares the read-only variable, which is equivalent to the readonly-x output variable (set as a global variable ), it is equivalent to export. if you change the prefix-to + of an option, you can delete a specific attribute. If no parameter is specified, all variables of a specific class or all are listed. Keyword variable HOME variable save user HOME directory ,~ Is expanded by shell as the HOME value. PATH specifies the shell search PATH. The search PATH consists of a group of paths separated. Export PATH will make it accessible to the quilt shell. $ Export PATH = $ PATH: $ HOME: # in the middle, the CDPATH and PATH syntax can be omitted. it extends the range of the cd command to the directory. PS1 sets the user's main prompt, PS2 sets the user's prompt, the default is>, PS3 sets the select control structure menu prompt, and PS4 sets the BASH debugging prompt. IFS sets the delimiter for Word Segmentation. by default, it is a blank character and does not need to be modified. For more keyword variables, refer to man bash manual. A function is also a variable. The syntax for creating a function is [function] name () {command}. the function puts a group of commands together to facilitate the call. Function names are the same as commands. to execute a function, you only need to execute the function name. Common functions can be stored in the profile file of BASH. The shell feature allows you to use set-o and set + o to enable and disable shell. For BASH features, see man bash.
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.