Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 根据行中的位置在shell中解析_Bash_Shell_Parsing - Fatal编程技术网

Bash 根据行中的位置在shell中解析

Bash 根据行中的位置在shell中解析,bash,shell,parsing,Bash,Shell,Parsing,我对脚本shell有一个小问题 让我们假设: pos=80:1 我希望在以下语法中使用此变量: while read line word=${line:pos} done < file.txt 读取行时 word=${line:pos} 完成

我对脚本shell有一个小问题

让我们假设:

pos=80:1
我希望在以下语法中使用此变量:

while read line

  word=${line:pos}

done < file.txt
读取行时
word=${line:pos}
完成
但那不行


准确地说,
word=$(行:80:1}
工作正常。

将其拆分并独立传递项目

pos=80:1
IFS=: read -r pos_start pos_len <<<"$pos"
word=${line:$pos_start:$pos_len}
pos=80:1

IFS=:read-r pos\u start pos\u len这是
bash
zsh
ksh
?它是在bash中编程的。请注意,如果
bash
以不同的方式解析事物,那么您就接近了:
${line:$pos如果在尝试解析
运算符之前扩展
bash
可以使用
操作。
但是,唉,它没有:)我会使用
IFS=:read pos_start pos_len@chepner,很好的呼叫;这在简洁性和可读性上都是一个胜利。已适当编辑。感谢您的帮助:)