Get first four characters from the variable name

Hi all

I have a variable name as below

runbatch=FCSTworLOADfx_CYR

Now i need to search for FCST in this variable and run a program

I am doing like this but it is failing

if [${RUNBATCH} |grep "FCST" == "FCST" ]; then ****RUN PROGRAM**

Please help

DJ

if [[ "$(echo $runbatch | grep FCST)" != "" ]]; then echo "find";fi
1 Like
if [[ $(grep "FCST" <<<${runbatch}) ]]; then ***RNU PROGRAM***
1 Like

This can all be done with standard variable expansions without needing to invoke utilities that aren't built into the shell:

if [ "${runbatch#*FCST}" != "$runbatch" ]
then    ****Run Program***
fi

You said run the program if FCST appears in $runbatch . If you meant if FCST appears as the 1st four characters in $runbatch , change "${runbatch#*FCST}" to "${runbatch#FCST}" .

Note that spaces around [ and ] are crucial and shell variable names are case sensitive.

1 Like