Take the first command line argument and pass the rest

Bash

Bash Problem Overview


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.

Bash Solutions


Solution 1 - Bash

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

Solution 2 - Bash

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[@]}"

Solution 3 - Bash

Adapting Abdullah's answer a bit:

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser554005View Question on Stackoverflow
Solution 1 - BashraviView Answer on Stackoverflow
Solution 2 - BashAbdullahView Answer on Stackoverflow
Solution 3 - BashHosam AlyView Answer on Stackoverflow