user554005
user554005

Reputation: 1323

Take the first command line argument and pass the rest

Example:

check_prog hostname.com /bin/check_awesome -c 10 -w 13

check_remote -H $HOSTNAME -C "$ARGS"
#To be expanded as
check_remote -H hostname.com -C "/bin/check_awesome -c 10 -w 13"

I hope the above makes sense. The arguments will change as I will be using this for about 20+ commands. Its a odd method of wrapping a program, but it's to work around a few issues with a few systems we are using here (gotta love code from the 70s).

The above could be written in Perl or Python, but Bash would be the preferred method.

Upvotes: 121

Views: 92433

Answers (3)

Abdullah
Abdullah

Reputation: 791

As a programmer I would strongly recommend against shift because operations that modify the state can affect large parts of a script and make it harder to understand, modify, and debug:sweat_smile:. You can instead use the following:

#!/usr/bin/env bash

all_args=("$@")
first_arg="$1"
second_args="$2"
rest_args=("${all_args[@]:2}")

echo "${rest_args[@]}"

Upvotes: 47

Hosam Aly
Hosam Aly

Reputation: 42464

Adapting Abdullah's answer a bit:

your_command "$1" "${@:2}"

Tested on Bash (v3.2 and v5.1) and Zsh (v5.8)

Upvotes: 40

ravi
ravi

Reputation: 3424

You can use shift

shift is a shell builtin that operates on the positional parameters. Each time you invoke shift, it "shifts" all the positional parameters down by one. $2 becomes $1, $3 becomes $2, $4 becomes $3, and so on

example:

$ function foo() { echo $@; shift; echo $@; } 
$ foo 1 2 3
1 2 3
2 3

Upvotes: 148

Related Questions