mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-03 23:40:14 +08:00
Merge pull request #5017 from GHLandy/master
[Translated] How To Assign Output of a Linux Command to a Variable
This commit is contained in:
commit
57013ee5cf
@ -1,81 +0,0 @@
|
||||
GHLandy Translating
|
||||
|
||||
How To Assign Output of a Linux Command to a Variable
|
||||
============================================================
|
||||
|
||||
When you run a command, it produces some kind of output: either the result of a program is suppose to produce or status/error messages of the program execution details. Sometimes, you may want to store the output of a command in a variable to be used in a later operation.
|
||||
|
||||
In this post, we will review the different ways of assigning the output of a shell command to a variable, specifically useful for shell scripting purpose.
|
||||
|
||||
To store the output of a command in a variable, you can use the shell command substitution feature in the forms below:
|
||||
|
||||
```
|
||||
variable_name=$(command)
|
||||
variable_name=$(command [option ...] arg1 arg2 ...)
|
||||
OR
|
||||
variable_name='command'
|
||||
variable_name='command [option ...] arg1 arg2 ...'
|
||||
```
|
||||
|
||||
Below are a few examples of using command substitution.
|
||||
|
||||
In this first example, we will store the value of `who` (which shows who is logged on the system) command in the variable `CURRENT_USERS` user:
|
||||
|
||||
```
|
||||
$ CURRENT_USERS=$(who)
|
||||
```
|
||||
|
||||
Then we can use the variable in a sentence displayed using the [echo command][1] like so:
|
||||
|
||||
```
|
||||
$ echo -e "The following users are logged on the system:\n\n $CURRENT_USERS"
|
||||
```
|
||||
|
||||
In the command above: the flag `-e` means interpret any escape sequences ( such as `\n` for newline) used. To avoid wasting time as well as memory, simply perform the command substitution within the [echo command][2] as follows:
|
||||
|
||||
```
|
||||
$ echo -e "The following users are logged on the system:\n\n $(who)"
|
||||
```
|
||||
[
|
||||
![Shows Current Logged Users in Linux](http://www.tecmint.com/wp-content/uploads/2017/01/Shows-Current-Logged-Users-in-Linux.png)
|
||||
][3]
|
||||
|
||||
Shows Current Logged Users in Linux
|
||||
|
||||
Next, to demonstrate the concept using the second form; we can store the total number of files in the current working directory in a variable called `FILES` and echo it later as follows:
|
||||
|
||||
```
|
||||
$ FILES=`sudo find . -type f -print | wc -l`
|
||||
$ echo "There are $FILES in the current working directory."
|
||||
```
|
||||
[
|
||||
![Show Number of Files in Directory](http://www.tecmint.com/wp-content/uploads/2017/01/Show-Number-of-Files-in-Directory.png)
|
||||
][4]
|
||||
|
||||
Show Number of Files in Directory
|
||||
|
||||
That’s it for now, in this article, we explained the methods of assigning the output of a shell command to a variable. You can add your thoughts to this post via the feedback section below.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
作者简介:
|
||||
|
||||
![](http://1.gravatar.com/avatar/4e444ab611c7b8c7bcb76e58d2e82ae0?s=128&d=blank&r=g)
|
||||
|
||||
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.tecmint.com/assign-linux-command-output-to-variable/
|
||||
|
||||
作者:[Aaron Kili][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.tecmint.com/author/aaronkili/
|
||||
[1]:http://www.tecmint.com/echo-command-in-linux/
|
||||
[2]:http://www.tecmint.com/echo-command-in-linux/
|
||||
[3]:http://www.tecmint.com/wp-content/uploads/2017/01/Shows-Current-Logged-Users-in-Linux.png
|
||||
[4]:http://www.tecmint.com/wp-content/uploads/2017/01/Show-Number-of-Files-in-Directory.png
|
@ -0,0 +1,81 @@
|
||||
如何将 Linux 命令的输出赋值给变量
|
||||
==================================
|
||||
|
||||
每当你运行一个命令,它都出在屏幕上输出一些内容:该命令的期望结果或者该命令执行细节的状态/错误消息。有些时候,你可能想要将某个命令的输出内容存储在一个变量中,以待在后续操作中取出来使用。
|
||||
|
||||
本文将介绍将 shell 命令赋值给变量的不同方法,这对于 shell 脚本编程是特别有用的。
|
||||
|
||||
你可以使用如下的 shell 命令置换特性来将命令的输出存储到变量中:
|
||||
|
||||
```
|
||||
变量名=$(命令)
|
||||
变量名=$(命令 [命令选项 ...] 参数1 参数2 ...)
|
||||
或者:
|
||||
变量名='命令'
|
||||
变量名='命令 [命令选项 ...] 参数1 参数2 ...'
|
||||
```
|
||||
|
||||
以下是使用命令置换特性的示例:
|
||||
|
||||
本例,我们将 `who` (显示当前登录系统的用户) 的输出值存储到 `CURRENT_USERS` 变量中:
|
||||
|
||||
```
|
||||
$ CURRENT_USERS=$(who)
|
||||
```
|
||||
|
||||
然后,我们可以使用 [echo 命令][1] 来使用上述变量,如下:
|
||||
|
||||
```
|
||||
$ echo -e "以下为登录到系统中的用户:\n\n $CURRENT_USERS"
|
||||
```
|
||||
|
||||
上面的命令中:`-e` 标记表示解释所有的转义序列 (如 `\n` 为换行)。为节约时间和内存,通常在 [echo 命令][2] 中使用命令置换特性,如下:
|
||||
|
||||
```
|
||||
$ echo -e "以下为登录到系统中的用户:\n\n $(who)"
|
||||
```
|
||||
|
||||
[![显示当前登录系统的用户](http://www.tecmint.com/wp-content/uploads/2017/01/Shows-Current-Logged-Users-in-Linux.png)][3]
|
||||
|
||||
在 Linux 中显示当前登录系统的用户
|
||||
|
||||
接下来,为了演示刚刚的第二种形式,我们以把当前工作目录下文件数存储到变量 `FILES` 为了,然后使用 echo 来输出,如下:
|
||||
|
||||
```
|
||||
$ FILES=`sudo find . -type f -print | wc -l`
|
||||
$ echo "当前目录有 $FILES 个文件。"
|
||||
```
|
||||
[![显示目中包含文件的数量](http://www.tecmint.com/wp-content/uploads/2017/01/Show-Number-of-Files-in-Directory.png)][4]
|
||||
|
||||
显示目中包含文件的数量
|
||||
|
||||
至此,文毕。我们展示了将 shell 命令的输出赋值给变量的方法。你可以在下边的评论反馈区留下你的想法。
|
||||
|
||||
---------------------------------------------------------
|
||||
|
||||
作者简介:
|
||||
|
||||
![](http://1.gravatar.com/avatar/4e444ab611c7b8c7bcb76e58d2e82ae0?s=128&d=blank&r=g)
|
||||
|
||||
Aaron Kili 是一名 Linux 和 F.O.S.S 忠实拥护者、高级 Linux 系统管理员、Web 开发者,目前在 TecMint 是一名活跃的博主,热衷于计算机并有着强烈的只是分享意愿。
|
||||
译者简介:
|
||||
|
||||
![GHLandy](http://GHLandy.com/images/GHLandy.ico)
|
||||
|
||||
[GHLandy](http://GHLandy.com) —— 欲得之,则为之奋斗 (If you want it, work for it.)。
|
||||
|
||||
------------------------------------------------
|
||||
|
||||
via: http://www.tecmint.com/assign-linux-command-output-to-variable/
|
||||
|
||||
作者:[Aaron Kili][a]
|
||||
译者:[GHLandy](https://github.com/GHLandy)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.tecmint.com/author/aaronkili/
|
||||
[1]:http://www.tecmint.com/echo-command-in-linux/
|
||||
[2]:http://www.tecmint.com/echo-command-in-linux/
|
||||
[3]:http://www.tecmint.com/wp-content/uploads/2017/01/Shows-Current-Logged-Users-in-Linux.png
|
||||
[4]:http://www.tecmint.com/wp-content/uploads/2017/01/Show-Number-of-Files-in-Directory.png
|
Loading…
Reference in New Issue
Block a user