如何将 cut -c 命令的输出存储到 shell 脚本中的变量中

how do I store the output of a cut -c command into a variable in shell script

我有一个像

这样的文件名

Incoming_file_180420053826.csv

其中 180420 代表日期,053826 代表时间。
我已经使用 echo $( cut -c 15-20 $filename ) 检索日期,但我无法将该输出日期存储在变量中。

尝试

my_var="$( echo  $filename | cut -c 15-20 )"

演示:

$filename=Incoming_file_180420053826.csv 
$my_var="$( echo  $filename | cut -c 15-20 )"
$echo $my_var 
180420
$
filename=test.txt
aa=$( cut -c 1-6 $filename )
echo $aa

180420 180421 180422 180423 180424 180425

但我认为您可能只需要第一行,也许这就是您所需要的。

bb=$(head -1 $file|cut -c 1-6)
echo $bb

180420