[rps-include post=6835]
[rps-include post = 6835]
Variables are programming part of the bash and very useful. Without variables bash will be very inefficient shell. Variables are used by bash itself, running applications, daemons, X (graphic) server and for a lot of things.
变量是bash编程的一部分,非常有用。 如果没有变量,bash将是非常低效的shell。 bash本身,运行应用程序,守护程序,X(图形)服务器以及许多其他东西都使用变量。
定义变量 (Define Variable)
Defining variables are easy in bash, just variable and data is enough. echo is used to print variables into terminal. With equal sign variable values are set. As you see page, Page, PAGE are different variables this means bash is case sensitive.
在bash中定义变量很容易,仅变量和数据就足够了。 echo用于将变量输出到终端。 设置等号变量值。 如您所见,page,Page,PAGE是不同的变量,这意味着bash区分大小写。
$ page="poftut.com"
$ PAGE="http://poftut.com"
$ Page="http://www.poftut.com"
$ echo $page
poftut.com
$ echo $Page
http://www.poftut.com
$ echo $PAGE
http://poftut.com
删除/取消定义变量 (Delete/Undefine Variable)
Sometimes we don’t need variables that are created before and we want simple delete them. unset keyword is used to delete variable completely.
有时我们不需要之前创建的变量,而是希望简单地删除它们。 unset关键字用于完全删除变量。
$ test=5
$ echo $test
5
$ unset test
$ echo $test
更改变量值(Change Variable Value)
There is no extra effort to change existing variable value. It is just like creating new variable
无需付出额外的努力来更改现有变量值。 就像创建新变量一样
$ page="poftut.com"
$ echo $page
poftut.com
$ page="www.poftut.com"
$ echo $page
www.poftut.com
串联变量 (Concatenate Variables)
To join variables together just put them together. In the second echo we put one space between hello and poftut variables which effects printed string too.
要将变量连接在一起,只需将它们放在一起即可。 在第二个回显中,我们在hello和poftut变量之间放置一个空格,这也会影响打印的字符串。
$ hello="Hello "
$ poftut="www.poftut.com"
$ echo $hello$poftut
Hello www.poftut.com
$ echo $hello" "$poftut
Hello www.poftut.com
We create two variables named hello and poftut. Than we echo both by joining together without any operator.In the second echo we put single space between them and then join all of them together.
我们创建两个名为hello和poftut的变量。 比我们通过不使用任何运算符将它们连接在一起来回显两者。
[rps-include post=6835]
[rps-include post = 6835]
翻译自: https://www.poftut.com/linux-bash-define-use-shell-variables/