Cut | command line args

Hi,

Can you please hint me how to achieve the below?

Input:
$./script.sh start 1 2

Internally inside the script i want to set a single variable with $2 and $3 value?

Output:

CMD=$1
ARGS=$2 $3

--VInodh

 
$ ./test.sh start 1 2 
CMD is : start
ARGS is : 1 2

 
$ cat test.sh
CMD=$1
ARGS="$2 $3"
echo "CMD is : $CMD"
echo "ARGS is : $ARGS"

Forgot to mention. Small change..

The first argument is fixed, but remaining no . of args may be incremental.

I mean, I want all the args $2 ...$n to be assigned to variable.

#! /bin/bash

CMD=$1
ARGS=${*:2:4}

echo $CMD
echo $ARGS
$ ./test.sh a b c d e f
a
b c d e
 
#!/bin/bash
count=0
for i in `echo $@`
do
[ "$count" != "0" ] && ARGS="$ARGS $i"
count=1
done
echo $ARGS
1 Like

@itkamaraj: works fine.

@balajesuri: Thanks for reply. But your solution restricted to 4 args, but it doesnt handle for n args.

Thanks for the both.

-VInodh

You should've been clear with the requirements. When you say from $2 to $n, it could mean from 2nd param to 6th param out of 10 params, right?

#! /bin/bash
CMD=$1
shift
ARGS=$*
echo $ARGS
1 Like
ARGS="$2""$3"

wrap with $2 and $3 if the contents of arguments contain special characters or spaces .

Hi,

You can do little bit modification from balajesuri's post

#! /bin/bash

CMD=$1
ARGS=${*:2:$#}

echo $CMD
echo $ARGS

Cheers,
Ranga:)

1 Like

Actually, quotes are only needed here if you want to add spaces or special characters but not if they are in the content of the variables. During assignment there is no expansion of the contents of the variables. This could also be written as:

ARGS=$2$3

However to add a space it would need to be:

ARGS="$2 $3"

To illustrate:

$ v='!@#$  %^     &*'
$ w=$v$v
$ printf "%s\n" "$v"
!@#$  %^     &*
$ printf "%s\n" "$w"
!@#$  %^     &*!@#$  %^     &*
1 Like

Thanks a lot scrutinizer for the enlightment .