get the first 5 characters from each line in shell script
If you want to use cut
this way, you need to use redirection <<<
(a here string) like:
var=$(cut -c-5 <<< "$line")
Note the use of var=$(command)
expression instead of id= cut -c-5 $line
. This is the way to save the command into a variable.
Also, use /bin/bash
instead of /bin/sh
to have it working.
Full code that is working to me:
#!/bin/bashfilename='sample.txt'while read -r linedo id=$(cut -c-5 <<< "$line") echo $id #code for passing id to other script file as parameterdone < "$filename"