View Full Version : Shell String Operations

May 5th, 2008, 08:11 PM
I am trying to use the following substring expression:


Except that I am trying to pass a variable as the length, and I can't quite figure out how to do it. Instead I get an error message:

sub1=${name1:1:${lffn}}: 0403-011 The specified substitution is not valid for this command.

Anyone know if this is possible, and how to do it?

Many thanks.

May 5th, 2008, 08:44 PM

sub1=`expr substr $name1 1 $lffn`

May 5th, 2008, 08:45 PM
I just tried a simple version of this, and it worked. Are you sure $lffn is a correct length? Are you sure you are using bash?

In the following I created a short script with no #! in the first line. I'll run the script with bash and with sh:

$ cat strtest.sh
echo $sub1
$ bash strtest.sh
$ sh strtest.sh
strtest.sh: 3: Syntax error: Bad substitution

May 6th, 2008, 08:06 PM
I'm actually using ksh - and the man pages I have available locally did not address string substitution at all.

As soon as I substituted my second expression it worked. I was also echoing all variables at this point, as I usually do when building a script - so I am quite sure that the variable was the correct length.