IQAndreas
IQAndreas

Reputation: 8468

Shell: Run other command with all but one parameter

I want to create a script name git-as which is basically an alias for calling git with a bunch of pre-defined parameters.

git --git-dir="$1/.git" --work-tree="$1/" <<OTHER PARAMS>>

When I want to call git in another repository, I use:

git-as "$SRC_REPO" commit -m "Changes made to $CLASS_NAME"

How do I get the "other params" bit working properly?

Keep in mind, I don't want the $1 param (containing the repository location) to be passed to git. Also, the other parameters may include double quotes such as the example shows them used.

Upvotes: 0

Views: 186

Answers (3)

Gordon Davisson
Gordon Davisson

Reputation: 125788

In bash (but not all other shells), you can use a variant on array slicing to pick out some of the arguments without having shift the others first:

#!/bin/bash
git --git-dir="$1/.git" --work-tree="$1/" "${@:2}"

Upvotes: 3

Maxpm
Maxpm

Reputation: 25572

Here's how I would do it:

#!/bin/bash
readonly Directory="$1"
shift
git --git-dir="$Directory/.git" "$@"

First, this copies the repository location ($1) to another variable ($Directory). Then it uses shift to move all the program arguments to the left by one, so $@ just contains the arguments that were intended for Git. From there, it's just a simple matter of calling Git with the new arguments.

Upvotes: 2

sotapme
sotapme

Reputation: 4903

Shell scripts allow the use of shift n to left shift the $*/$@ args which may suit your purpose.

# t.sh
echo ARGS $*
shift 
echo ARGS $*

bash t.sh 1 2 3 4 5 gives

ARGS 1 2 3 4 5
ARGS 2 3 4 5

Upvotes: 2

Related Questions