Https://www.shellscript.sh/variables1.html
A variable is the name of a block of memory that can be manipulated (read, write).
1, return to the tradition. -"The only way to learn a new programming language is to use it to program"try -1
Create a variable:
[Email protected]:~/labs# sh mytest. SH Hello. [email protected]: ~/labs# Cat mytest. SH #!/bin/ bashmy_message="Hello. " " Echo $MY _message
There should be no spaces on both sides of the assignment number! ~
try -2
Reads a line from the standard input:
[Email protected]:~/labs# sh mytest. SH What is YOUR name?xkfxhello xkfx- HOPE you. [email protected]: ~/labs# Cat mytest. SH #!/bin/bash Echo What is YOUR NAME? Read My_name Echo " Hello $MY _name-hope you is well. "
2. About variableswhat exactly is inside the variable?
Everything is "string".
If you think sum is a number, it's a big mistake.
Whether it's an integer, a real number, or whatever type you like,
are stored as strings.
Just let the interpreter think of the variable as ...
(referencing:P backwards)
Scope of the variable
export and . (dot) It's two very common commands that make it easy to understand the scope of variables by figuring out what these two commands do for the computer.
Ⅰ, about export:
The Export command is used to output a shell variable as an environment variable, or to output a shell function as an environment variable. When a variable is created, it is not automatically known to the shell process that was created after it. The command export can pass the value of the variable to the following shell. When a shell script is called and executed, it does not automatically get access to the variables defined in the script (the caller) unless the variables have been explicitly set to available. The Export command can be used to pass the value of one or more variables to any subsequent script. from: http://man.linuxde.net/export
The mytest.sh code is below! Down there!
[email protected]:~/labs# export msg="aaaaaaaa"[email protected]:~/labs# sh mytest. SH msg=aaaaaaaamsg=value-2
Ⅱ, once a script has been executed, the "Environment", "variables" it creates will be destroyed, and we can refer to the script by using. Instead of creating a new shell to run it :
[Email protected]:~/labs# CatMyTest.SH #!/bin/BashEcho "msg= $MSG"MSG="VALUE-2"Echo "msg= $MSG"[Email protected]:~/labs#msg="VALUE-1"[Email protected]:~/labs# SHMyTest.SH # RUN THROUGH sh COMMAND, which CREATE A NEW SHELL to RUN IT. MSG =msg=value-2[Email protected]:~/labs#. MyTest.SH # RUN THROUGH. COMMAND, which USES current SHELL to RUN IT. MSG =value-1msg=value-2
3. When to use
${} ?
Not many words said:
#!/bin/bash echo " what is your name? " read user_name echo " hello $USER _name " echo " i would create you a file called ${user_name}_file " touch " ${user_name}_file
[Email protected]:~/labs# sh mytest. SH What is your Name?xkfxhello xkfxi would create you a file called Xkfx_file[email protected]: ~/labs# ls mytest.sh xkfx_file
4, learning without thinking is bewildered
To-do
30 minutes per day
Next: Variables you don't know
"LINUX SHELL" Note 02: initial knowledge of variables