DEV Community 👩‍💻👨‍💻

Cover image for Forward arguments in shell
Derk-Jan Karrenbeld
Derk-Jan Karrenbeld

Posted on • Originally published at

Forward arguments in shell

Originally published at I have recently taken up writing, again and am trying to commit to at least one helpful post each week for the next 52 weeks.

For one of my CLIs, I needed to remove the first argument from the argument list and forward the rest to a next process. In this example, my shell script is called cli_process and given arguments arg1, arg2 and arg3, I want to execute forward_process with arg2 and arg3.

#!/usr/bin/env bash

# Documentation on @
# Documentation on shift [n]
# Why quoting the argument is "necessary":

# eats 1 argument
shift 1

# forwards the rest to "forward_process"
forward_process "$@"

Originally posted as a gist, by myself.


Windows .bat files support a similar token: %*, which is equivalent to $@.
However, the SHIFT command, unlike the bash equivalent shift, doesn't modify this special token. There are various solutions that will attempt to eat the first n parameters, but all of them have edge-cases in which they don't properly work. Should you need this in windows, I recommend you write out the arguments manually %2, %3 (and skip %1).

Here you can find some Windows solutions, but make sure you check the comments underneath each one:


Want these short articles in your e-mail? 📧 Sign up for my newsletter.

Top comments (2)

ferricoxide profile image
Thomas H Jones II

Heh... if you're someone that implements flag/args in bash, you're familiar with shift because it's how you parse getopt strings.

sleeplessbyte profile image
Derk-Jan Karrenbeld

Absolutely! But when I needed to do this, I did not have that knowledge yet. Only needed to drop an argument and pass it on to the next binary :).

Shift is great

Every Week

We have a Welcome Thread where we invite members to tell us a bit about themselves. Join the conversation with us!