Search code examples
bashshellvariablesunixcut

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


I have a file name like

Incoming_file_180420053826.csv

where 180420 represents the date and 053826 represents time.
I have used echo $( cut -c 15-20 $filename ) to retrieve the date, but I'm unable to store that output date in a variable.


Solution

  • Try

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

    Demo:

    $filename=Incoming_file_180420053826.csv 
    $my_var="$( echo  $filename | cut -c 15-20 )"
    $echo $my_var 
    180420
    $