2

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?

F. Hauri - Give Up GitHub
  • 64,122
  • 17
  • 116
  • 137
Dharmesh Mehta
  • 83
  • 1
  • 2
  • 7
  • 3
    It's not clear what you mean. `${@:3}` means the rest of the arguments; how could `$4` not be a part of that list? In the trivial case, reorder the arguments and use `$3` for `DATAVERSION` and `${@:4}` for `DATA`. (Also, tangentially, [don't use upper case for your private variables.)](https://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization) – tripleee Nov 08 '21 at 06:28

1 Answers1

4

command line:

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

For this, simply open any terminal window and try :

set -- "Full Name" "password" csv V1

Then

echo $1
Full Name

echo ${@:3}
csv V1

echo ${@:0:3}
/bin/bash Full Name password

echo ${@:1:3}
Full Name password csv

so

userName=$1
passWord=$2
datas=${*:3}
dataVersion=$4

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

Must produce:

userName     <Full Name>
passWord     <password>
datas        <csv V1>
dataVersion  <V1>

Using shift

Or ...

set -- "Full 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     <Full Name>
passWord     <password>
datas        <csv V1>
dataVersion  <V1>

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

F. Hauri - Give Up GitHub
  • 64,122
  • 17
  • 116
  • 137
  • Some people call that dromedaryCase (camels have two humps, dromedaries just one). – tripleee Nov 08 '21 at 07:32
  • @tripleee Yes, *dromedaryCase* or *lowerCamelCase* (with two humps). Anyway important thing is: it's not ALLCAPS! (And remain more readable) – F. Hauri - Give Up GitHub Nov 08 '21 at 07:44
  • No, no, only bactrian camels have two humps. So in that sense, since dromedary camels have only one -- camelCase can be ambiguous. But still when compared between "camelCase" and "TitleCase", etc.. the meaning is fairly obvious. Simply using lowercase avoids the whole camel discussion `:)` – David C. Rankin Nov 08 '21 at 08:03
  • @DavidC.Rankin In french, there is no ambiguity: Camel is *chameau* and have **two** humps. More informations about: [kINd oF caPiTaLiSAtIOn](https://english.stackexchange.com/q/533036/62976) – F. Hauri - Give Up GitHub Nov 08 '21 at 08:11
  • Thanks, I'll keep a note of that to use a proper naming convention – Dharmesh Mehta Nov 08 '21 at 08:14
  • I've been schooled... I would have never thought folks put so much time into defining case. The "Studly case" was a completely new use of the "Studly" and in my mid 50s with 25 years practicing law -- I thought I had seen it all -- nope... – David C. Rankin Nov 08 '21 at 08:19
  • @DavidC.Rankin So U won't be surprised that bash's man page was recently patched to avoid gendered expression. https://lists.gnu.org/archive/html/bug-bash/2021-06/msg00016.html – F. Hauri - Give Up GitHub Nov 08 '21 at 12:09
  • ......... touché – David C. Rankin Nov 08 '21 at 18:36