0

suppose the user passed 5 arguments($1 $2 $3 $4 $5), how do I cut off the first two arguments($1 $2) and I only want the last 3 arguments

awk '{print $3,$4,$5} < $@' what if I don't know how many arguments passed but I do not want the first two arguments

I can not use shift since I also need info from $2, what should I do then?

I ran into this problem when tar files in bash script when the user might use regular expressions like *.txt and the shell will automatically convert that into file1.txt file2.txt (suppose there are only two .txt files)

Bob
  • 3
  • 4

1 Answers1

1

There is a syntax for this:

#!/bin/bash

# note: indexing starts at 0

# 0: script name
# 1: arg 1
# 2: arg 2

# this will skip the script name, arg1 and arg2
echo "${@:3}"
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • if I do something like `tar -cvf $2 ../source/${@:3)` the indicator became red for the following code, is this normal – Bob Oct 06 '21 at 19:54
  • 2
    @Bob You used a close parenthesis rather than curly brace. Also, are you trying to prepend "../source/" to *each* argument (starting with `$3`), or just to `$3` (and `$4`, `$5`, etc don't get the prefix)? Finally, you should put double-quotes around parameter and variable references, including `$2` and `$@`. – Gordon Davisson Oct 06 '21 at 20:32