Understanding and learning about Linux Shell
The Linux terminal executes commands through the bash environment. Bash includes setting and using variables, constructing the bash operating environment, and redirecting data streams. The following knowledge helps you maintain and manage hosts.
The operating system kernel is used to manage the entire computer hardware. This kernel is protected. Generally, users can only communicate with the kernel through shell (shell communicates the input command with the kernel, the kernel can control the hardware to work correctly ). Commands such as man, chmod, chown, vi, and mkfs are independent applications. We can operate these applications through shell (that is, the command line mode, let these applications call the kernel to run the required work.
Bash shell built-in command: type
Type command to find executable files, rather than general file names.
Bkjia @ Ubuntu :~ /Desktop/linux_study $ type ls # list the main usage of ls without adding any parameters
Ls is aliased to 'ls -- color = auto'
Bkjia @ ubuntu :~ /Desktop/linux_study $ type-t ls # Add the parameter-t to list the basis for ls execution
Alias
Bkjia @ ubuntu :~ /Desktop/linux_study $ type-a ls # Add the parameter-a to list all the commands containing ls from the path variable definition, including alias
Ls is aliased to 'ls -- color = auto'
Ls is/bin/ls
Bkjia @ ubuntu :~ /Desktop/linux_study $ type cd
Cd is a shell builtin # cd is a shell built-in command
Command Execution
In many cases, if the input command is too long, \ will be added before the line break. This is done to transfer the line break [enter. After successful transfer, the> symbol will appear at the beginning of the next line. You can continue to enter the command.
Shell Variable Function
A variable uses a specific string to indicate unfixed content. To display the variable content in linux, you can use the echo command to display the variable. However, when the variable is displayed, you must add $.
Bkjia @ ubuntu :~ /Desktop/linux_study $ echo $ variable
Bkjia @ ubuntu :~ /Desktop/linux_study $ echo $ PATH
/Usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games: /usr/local/games
Set Variables
Bkjia @ ubuntu :~ /Desktop/linux_study $ echo $ myname
Bkjia @ ubuntu :~ /Desktop/linux_study $ myname = flora
Bkjia @ ubuntu :~ /Desktop/linux_study $ echo $ myname
Flora
Retain the original variables and set them.
Bkjia @ ubuntu :~ /Desktop/linux_study $ myname = "$ myname lin"
Bkjia @ ubuntu :~ /Desktop/linux_study $ echo $ myname
Flora lin
During the execution, the commands in the backticks (') are executed first, and the execution results are used as external input information. The locate command can list all related file names, but if we want to know the permission for each crontab Related File Name:
Bkjia @ ubuntu :~ /Desktop/linux_study $ ls-l 'locate crontab'
-Rw-r -- 1 root 401 Feb 19 2014/etc/anacrontab
-Rw-r -- 1 root 722 Feb 8 2013/etc/crontab
-Rwxr-sr-x 1 root crontab 34824 Feb 8 2013/usr/bin/crontab
-Rw-r -- r-1 root 1184 Apr 7 2014/usr/share/bash-completion/completions/crontab
-Rw-r -- 1 root 27684 Feb 8 2013/usr/share/doc/cron/examples/crontab2english. pl
-Rw-r -- 1 root 2521 Feb 8 2013/usr/share/man/man1/crontab.1.gz
-Rw-r -- 1 root 892 Feb 19 2014/usr/share/man/man5/anacrontab.5.gz
-Rw-r -- 1 root 5687 Feb 8 2013/usr/share/man/man5/crontab.5.gz
First, use the locate command to list the file name data and then use the ls command for processing.
Functions of Environment Variables
Environment variables can help us achieve many functions, including main folder transformation, prompt display, and execution file search path.
View the default environment variables:
Bkjia @ ubuntu :~ /Desktop/linux_study $ env
#......
_ =/Usr/bin/env # The last parameter of the last command used (or the command itself)
Random Number
The content of the RANDOM variable is between 0 and 0 ~ Between 32767, so a numerical value is required to obtain a single digit.
Bkjia @ ubuntu :~ /Desktop/linux_study $ declare-I number = $ RANDOM * 10/32768; echo $ number
1
Bkjia @ ubuntu :~ /Desktop/linux_study $ declare-I number = $ RANDOM * 10/32768; echo $ number
4
Bkjia @ ubuntu :~ /Desktop/linux_study $ declare-I number = $ RANDOM * 10/32768; echo $ number
5
Set to view all variables (including environment variables and custom variables)
Bash does not only have environment variables, but also has some important parameters and User-Defined variables.
Bkjia @ ubuntu :~ /Desktop/linux_study $ set
PS1 Command Prompt
Every time we press the Enter key to execute a command, the prompt will appear again and we will actively read the variable value.
Bkjia @ ubuntu :~ /Desktop/linux_study $ cd/home
Bkjia @ ubuntu:/home $ PS1 = '[\ u @ \ h \ w \ A #\#] \ $'
[Bkjia @ ubuntu/home 08:48 #46] $ ls
Bkjia
[Bkjia @ ubuntu/home 08:51 #47] $
\ W indicates the complete working directory name, which is written by a directory. But the main folder will use ~ .
\ A display time, in the 24-hour format of "HH: MM ".
# Run the following commands.
\ $ Prompt. If it is root, the prompt is #; otherwise, the prompt is $.
Variable keyboard reading, array and Declaration: read, array, declare
Read reads the variables from the keyboard.
[Bkjia @ ubuntu/home #54] $ read atest
This is a test
[Bkjia @ ubuntu/home 09:12 #55] $ read-p "Please keyin your name:"-t 30 named
Please keyin your name: bkjia
[Bkjia @ ubuntu/home 09:13 #56] $ echo $ named
Bkjia
If no operation is performed within 30 seconds, the command is skipped automatically.
Declare or typeset to declare the type of the Variable
If declare is followed by no parameters, bash automatically counts the variable names.
[Bkjia @ ubuntu/home #59] $ sum = 100 + 300 + 50
[Bkjia @ ubuntu/home #60] $ declare-I sum = 100 + 300 + 50
[Bkjia @ ubuntu/home 09:22 #61] $ echo $ sum
450
The default variable type is a string. Therefore, if you cannot specify the variable type, 1 + 2 is a string instead of a formula. Therefore, the First Command will output 100 + 300 + 50 ..
Change sum to an environment variable:
[Bkjia @ ubuntu/home 09:13 #57] $ declare-x sum
[Bkjia @ ubuntu/home 09:21 #58] $ export | grep sum
Declare-x sum
Convert sum to read-only attribute and cannot be modified:
[Bkjia @ ubuntu/home 09:25 #62] $ declare-r sum
[Bkjia @ ubuntu/home 09:29 #63] $ sum = testgting
Bash: sum: readonly variable
Change sum to a non-environment variable:
[Bkjia @ ubuntu/home 09:29 #64] $ declare + x sum
[Bkjia @ ubuntu/home 09:30 #65] $ declare-p sum
Declare-ir sum = "450"
Array variable type
[Bkjia @ ubuntu/home 09:30 #66] $ var [1] = "small min"
[Bkjia @ ubuntu/home 09:32 #67] $ var [2] = "big min"
[Bkjia @ ubuntu/home 09:32 #68] $ var [3] = "nice min"
[Bkjia @ ubuntu/home #69] $ echo "$ {var [1]}, $ {var [2]}, $ {var [3]}"
Small min, big min, nice min
Restrictions on file systems and programs ulimit
List all restricted data values of your current identity:
[Bkjia @ ubuntu/home 09:33 #70] $ ulimit-
Restrict the creation of only 10 MB capacity files:
[Bkjia @ ubuntu/home #74] $ ulimit-f 10240
[Bkjia @ ubuntu/home #75] $ ulimit-
Try to create a 20 m file:
[Bkjia @ ubuntu ~ /Desktop/linux_study #81] $ sudo dd if = ~ /Desktop _study/of = 123 bs = 1 M count = 20
[Bkjia @ ubuntu ~ /Desktop/linux_study #82] $ ll
Total 8
Drwxrwxr-x 2 bkjia 4096 Oct 30 ./
Drwxr-xr-x 3 bkjia 4096 Oct 30 ../
-Rw-r -- 1 root 0 Oct 30 09:41 123
Deletion, replacement and substitution of variable content
Delete and replace variable content
Delete the local directories:
[Bkjia @ ubuntu ~ /Desktop/linux_study #85] $ echo $ path
/Usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games: /usr/local/games
[Bkjia @ ubuntu ~ /Desktop/linux_study 09:49 #89] $ echo $ {path #/* local/bin :}
/Usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
Replace sbin with SBIN:
Bkjia @ ubuntu ~ /Desktop/linux_study 13:06 #95] $ echo $ {path/sbin/SBIN}
/Usr/local/SBIN:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games: /usr/local/games
[Bkjia @ ubuntu ~ /Desktop/linux_study 13:06 #96] $ echo $ {path // sbin/SBIN}
/Usr/local/SBIN:/usr/local/bin:/usr/SBIN:/usr/bin:/SBIN:/bin:/usr/games: /usr/local/games
Test and content replacement of variables:
[Bkjia @ ubuntu ~ /Desktop/linux_study 13:07 #97] $ echo $ username
# Username may not exist or be a Null String
[Bkjia @ ubuntu ~ /Desktop/linux_study 13:09 #98] $ username =$ {username-root}
[Bkjia @ ubuntu ~ /Desktop/linux_study 13:10 #99] $ echo $ username
Root # No settings, so take the initiative to give the content named root
[Bkjia @ ubuntu ~ /Desktop/linux_study #100] $ username = "bkjia"
[Bkjia @ ubuntu ~ /Desktop/linux_study 13:10 #101] $ username =$ {username-root}
[Bkjia @ ubuntu ~ /Desktop/linux_study #102] $ echo $ username
Bkjia
Username-root gets the variable, the option to be tested; content of the username-root variable, in this example, this part is giving the content of the variable not set. Of course, username:-root can replace the following content if the variable content is empty or not set.
If the old variable does not exist, replace it with the new variable content. If the old variable exists, replace the new variable content with the old one.
[Bkjia @ ubuntu ~ /Desktop/linux_study #103] $ unset str; var =$ {str-newvar}
[Bkjia @ ubuntu ~ /Desktop/linux_study #104] $ echo var = "$ var", str = "$ str"
Var = newvar, str = # Because str does not exist, var is newvar
[Bkjia @ ubuntu ~ /Desktop/linux_study #105] $ str = "oldvar"; var =$ {str-newvar}
[Bkjia @ ubuntu ~ /Desktop/linux_study #106] $ echo var = "$ var", str = "$ str"
Var = oldvar, str = oldvar # Because str exists, var is equal to str content
Subtraction does not affect the old variables. If you want to replace the old variables together, you need to use equal signs =:
[Bkjia @ ubuntu ~ /Desktop/linux_study #107] $ unset str; var =$ {str = newvar}
[Bkjia @ ubuntu ~ /Desktop/linux_study #108] $ echo var = "$ var", str = "$ str"
Var = newvar, str = newvar
[Bkjia @ ubuntu ~ /Desktop/linux_study #111] $ str = "oldvar"; var =$ {str = newvar}
[Bkjia @ ubuntu ~ /Desktop/linux_study #112] $ echo var = "$ var", str = "$ str"
Var = oldvar, str = oldvar
Command alias and History commands
Set alias and unalias command alias
The command alias can be used to change frequently-used but particularly long commands to concise commands, and the default options are added to some common commands, it can prevent accidental deletion of files. For example, to query hidden files and to list them for a long time and view them on one page, you need to execute "ls-l | more". You can use the following command to simplify it to lm:
[Bkjia @ ubuntu ~ #120] $ alias lm = "ls-l | more"
In addition, the alias settings can replace existing commands. For example, when you log on as root, you can use the rm command to delete all files. Therefore, be careful, rm provides a parameter for us to confirm whether to delete the file-I:
[Bkjia @ ubuntu ~ #123] $ alias rm = "rm-I"
[Bkjia @ ubuntu ~ /Desktop/linux_study #127] $ rm 123
Rm: remove write-protected regular empty file '20140901 '? Y
Remove the alias:
[Bkjia @ ubuntu ~ /Desktop/linux_study #128] $ unalias rm
History command history
Query the last four commands that have been executed:
[Bkjia @ ubuntu ~ /Desktop/linux_study #131] $ history 4
Bash Shell operating environment
Path and command query order
Set the echo alias to echo-n, and then view the echo execution sequence:
[Bkjia @ ubuntu ~ /Desktop/linux_study #132] $ alias echo = "echo-n"
[Bkjia @ ubuntu ~ /Desktop/linux_study #134] $ type-a echo
Echo is aliased to 'echo-N'
Echo is a shell builtin
Echo is/bin/echo
Bash logon and welcome information:/etc/issue,/etc/motd
Terminal Environment Settings: stty. set
How can I check the current key content? You can use stty (setting tty terminal) to help set the input button of the terminal to indicate the meaning. We can use stty-a to list all the buttons in the current environment.
Bkjia @ ubuntu :~ $ Stty-
Speed 38400 baud; rows 37; columns 80; line = 0;
Intr = ^ C; quit = ^ \; erase = ^ ?; Kill = ^ U; eof = ^ D; eol = M-^ ?; Eol2 = M-^ ?;
Swtch = M-^ ?; Start = ^ Q; stop = ^ S; susp = ^ Z; rprnt = ^ R; werase = ^ W;
Lnext = ^ V; flush = ^ O; min = 1; time = 0;
-Parenb-parodd cs8 hupcl-cstopb cread-clocal-crtscts
-Ignbrk brkint-ignpar-parmrk-inpck-istrip-inlcr-igncr icrnl ixon-ixoff
-Iuclc ixany imaxbel iutf8
Opost-olcuc-ocrnl onlcr-onocr-onlret-ofill-ofdel nl0 cr0 tab0 bs0 vt0 ff0
Isig icanon iexten echo echoe echok-echonl-noflsh-xcase-tostop-echoprt
Echoctl echoke
If you want to delete stty related characters:
Bkjia @ ubuntu :~ $ Stty erase ^ h
Apart from stty, bash also has its own terminal setting value set.
Linux Shell script Multithreading
Linux Shell uses read to input from the keyboard while
Linux Shell program debugging
Linux Shell script interview 25 questions
Pass Linux/Unix Shell parameters to SQL scripts
Introduction to parameter passing methods in Shell scripts
PASS command line parameters through Shell scripts
Linux Shell wildcards, escape characters, metacharacters, and special characters
This article permanently updates the link address: