bash 如何在bash中剪切现有变量并分配给新变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47406014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to cut an existing variable and assign to a new variable in bash
提问by Berkay K?rm?z?o?lu
!/bin/bash
VAR=$(some curl commands)
token =$(cut -c 18-53 $VAR)
echo $token
I want to use VAR variable in my cut command but, when I use like this it says;
我想在我的 cut 命令中使用 VAR 变量,但是,当我这样使用时,它说;
No such file or directory
I just wanna cut VAR(output of curl command) from 18.char to 53.char. Any suggestion ?
我只是想将 VAR(curl 命令的输出)从 18.char 削减到 53.char。有什么建议吗?
回答by John1024
Let's define an example var
:
让我们定义一个例子var
:
$ var='The quick brown fox jumped over the lazy dog on the way to the market'
Now let's select characters 18 through 53 using cut
:
现在让我们使用 选择字符 18 到 53 cut
:
$ echo $(cut -c 18-53 <<<"$var")
ox jumped over the lazy dog on the w
Because cut
expects to read from standard input (if not a file), we use <<<
to tell bash to provide the contents of $var
on standard input. This is called a here string.
因为cut
期望从标准输入(如果不是文件)读取,我们使用<<<
告诉 bash 提供$var
标准输入的内容。这称为此处字符串。
Alternatively, we can select the same characters using bash alone:
或者,我们可以单独使用 bash 选择相同的字符:
$ echo ${var:17:36}
ox jumped over the lazy dog on the w
The construct ${var:17:36}
is called substring expansion. It selects 36 characters starting from position 17. (In bash, unlike cut
, the first character is numbered zero.)
该构造${var:17:36}
称为子字符串扩展。它从位置 17 开始选择 36 个字符。(在 bash 中,与 不同cut
,第一个字符编号为零。)
We can, of course, assign the selected string to a variable:
当然,我们可以将选定的字符串分配给一个变量:
$ token=${var:17:36}
$ echo "$token"
ox jumped over the lazy dog on the w
Or:
或者:
$ token=$(cut -c 18-53 <<<"$var")
$ echo "$token"
ox jumped over the lazy dog on the w
POSIX
POSIX
The above commands work in bash. If we want portability to POSIX shells, then we can use neither substring expansionnor here strings. Instead, as Gordon Davissonpoints out, we can use:
上述命令适用于 bash。如果我们想要 POSIX shell 的可移植性,那么我们既不能使用子字符串扩展,也不能使用这里的 strings。相反,正如戈登戴维森指出的那样,我们可以使用:
$ echo "$var" | cut -c 18-53
ox jumped over the lazy dog on the w
or:
或者:
$ token=$(echo "$var" | cut -c 18-53)
$ echo "$token"
ox jumped over the lazy dog on the w
gniourf_gniourfsuggests yet another POSIX method, this one avoiding external processes:
gniourf_gniourf提出了另一种 POSIX 方法,这个方法避免了外部进程:
$ printf '%.36s\n' "${var#?????????????????}"
ox jumped over the lazy dog on the w
Comparison of cut
and bash substring expansion
cut
和bash子串扩展的比较
As David C. Rankinpoints out in the comments, there are strong advantages to uses bash's internal string handling. One is that the use of bash's internal commands avoids the spawning of additional subshells and executables. If the additional subshells are spawned within a loop, this can greatly impact performance.
正如David C. Rankin在评论中指出的那样,使用 bash 的内部字符串处理有很大的优势。一是使用 bash 的内部命令避免了产生额外的子 shell 和可执行文件。如果在循环中产生额外的子外壳,这会极大地影响性能。
Also, command substitution has the side-effect of removing trailing newlines from its output. This can cause unwanted surprises. Using bash's internal string handling avoids this side-effect.
此外,命令替换具有从其输出中删除尾随换行符的副作用。这可能会导致意外的意外。使用 bash 的内部字符串处理可以避免这种副作用。