I am running a shells script with 3 arguments. I am getting the word count of each one. But I want to skip over the very first argument, maybe by using shift or something else.

Most of the examples I've seen using shift involve for/while loops, which is more than I need for what I'm doing.

The below is within an if-statement, not necessary to show. Is there a simple/quick way to have $@ shift over one?

wc $@
for((i=2;i<=$#;i++)); do
    wc "${!i}"
  • Note that C-style for loop only works in bash, zsh and ksh93. – cuonglm Jan 21 '15 at 9:32

Just calling shift with argument 1 and remember to quote "$@":

shift 1
wc "$@"
  • 1
    I tried that already, it doesn't work. Something I left out of the title is I am trying to append the results to a file. So I currently am doing: wc "$@" >> $1, but I am getting nothing in there, even with shift 1 before. – eltigre Jan 21 '15 at 3:22
  • 1
    @eltigre: because you have shifted, $1 is no longer what you enter in command line, you must save it first. If you can provide your full script, we can make it more clearly. – cuonglm Jan 21 '15 at 3:24

Save the first argument in a variable, then shift once:

wc "$@" >"$arg1"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.