Home > Mobile >  How to accept argument after ${@:3} shell script
How to accept argument after ${@:3} shell script

Time:11-08

I have specified variables in the shell script as follows

USERNAME="$1"
PASSWORD="$2"
DATA="${@:3}"
DATAVERSION="$4"

when I run the script is goes something like this:

./abc.sh "name" "password" csv V1

But when here csv and V1 are considered as 3rd argument instead of considering V1 as the fourth argument Because of ${@:3} which I needed.

So how one can end this ${@:3} while passing the argument to script. So that arguments can be read?

CodePudding user response:

command line:

One of most powerfull feature of bash is: You could try inline near every part of your script.

For this, simply try:

set -- "name" "password" csv V1

Then

echo $1
name

echo ${@:3}
csv V1

echo ${@:0:3}
bash name password

echo ${@:1:3}
name password csv

Using shift

Or ...

set -- "name" "password" csv V1

Then

userName=$1
shift
passWord=$1
shift
datas=$*
shift
dataVersion=$1

printf '%-12s <%s>\n' userName "$userName" passWord "$passWord" \
                      datas "$datas" dataVersion "$dataVersion"

will produce:

userName     <name>
passWord     <password>
datas        <csv V1>
dataVersion  <V1>

Regarding tripleee's comment about capitalized variable names, my preference I to use lowerCamelCase.

  • Related