如何在bash中剪切现有变量并分配给新变量 [英] How to cut an existing variable and assign to a new variable in bash
问题描述
!/bin/bash
VAR=$(some curl commands)
token =$(cut -c 18-53 $VAR)
echo $token
我想在我的cut命令中使用VAR变量,但是当我这样使用时,它说;
I want to use VAR variable in my cut command but, when I use like this it says;
No such file or directory
我只是想将VAR(curl命令的输出)从18.char削减到53.char.有什么建议吗?
I just wanna cut VAR(output of curl command) from 18.char to 53.char. Any suggestion ?
推荐答案
让我们定义一个示例 var
:
$ var='The quick brown fox jumped over the lazy dog on the way to the market'
现在让我们使用 cut
选择字符18到53:
Now let's select characters 18 through 53 using cut
:
$ echo $(cut -c 18-53 <<<"$var")
ox jumped over the lazy dog on the w
由于 cut
希望从标准输入(如果不是文件)中读取内容,因此我们使用<<<
告诉bash提供的内容> $ var
在标准输入上.这称为此处字符串.
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.
或者,我们可以单独使用bash选择相同的字符:
Alternatively, we can select the same characters using bash alone:
$ echo ${var:17:36}
ox jumped over the lazy dog on the w
构造 $ {var:17:36}
被称为子字符串扩展.它从位置17开始选择36个字符.(在bash中,与
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.)
我们当然可以将选定的字符串分配给变量:
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
或者:
$ token=$(cut -c 18-53 <<<"$var")
$ echo "$token"
ox jumped over the lazy dog on the w
POSIX
以上命令在bash中有效.如果我们想移植到POSIX外壳,则不能使用 substring扩展或此处字符串.相反,正如 Gordon Davisson 指出的那样,我们可以使用:
POSIX
The above commands work in bash. If we want portability to POSIX shells, then we can use neither substring expansion nor here strings. Instead, as Gordon Davisson points out, we can use:
$ echo "$var" | cut -c 18-53
ox jumped over the lazy dog on the w
或:
$ token=$(echo "$var" | cut -c 18-53)
$ echo "$token"
ox jumped over the lazy dog on the w
gniourf_gniourf 提出了另一种POSIX方法,该方法避免了外部过程:
gniourf_gniourf suggests yet another POSIX method, this one avoiding external processes:
$ printf '%.36s\n' "${var#?????????????????}"
ox jumped over the lazy dog on the w
cut
和bash子字符串扩展的比较
David C. Rankin 在评论中指出,使用bash的内部结构具有强大的优势字符串处理.一种是使用bash的内部命令可避免产生其他子shell和可执行文件.如果在循环内生成其他子外壳,则可能会极大地影响性能.
Comparison of cut
and bash substring expansion
As David C. Rankin points 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.
此外,命令替换还有从输出中删除尾随换行符的副作用.这可能会导致意外的意外.使用bash的内部字符串处理可避免这种副作用.
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中剪切现有变量并分配给新变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!