Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all but first N arguments to a bash function

I'm aware of how to get the last argument passed to a function but I'm wondering how to get all the arguments of a function after the first two:

For instance:

function custom_scp(){
    PORT=$1
    USER=$2
    SOURCES=`ALL_OTHER_ARGS`
    scp -P $PORT -r $SOURCES [email protected]:~/
}

So sending three files to the remote home directory would look like

$ custom_scp 8001 me ./env.py ./test.py ./haha.py
like image 946
ted Avatar asked Feb 13 '19 20:02

ted


People also ask

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

What does $# in bash mean?

$# is the number of positional parameters passed to the script, shell, or shell function. This is because, while a shell function is running, the positional parameters are temporarily replaced with the arguments to the function. This lets functions accept and use their own positional parameters.

What is $@ and $* in shell script?

"$@" Stores all the arguments that were entered on the command line, individually quoted ("$1" "$2" ...). So basically, $# is a number of arguments given when your script was executed. $* is a string containing all arguments. For example, $1 is the first argument and so on.

How do I pass multiple parameters in bash?

Passing multiple arguments to a bash shell script In general, here is the syntax of passing multiple arguments to any bash script: script.sh arg1 arg2 arg3 … The second argument will be referenced by the $2 variable, the third argument is referenced by $3 , .. etc.

How to access the first Bash argument in a script?

The first bash argument (also known as a positional parameter) can be accessed within your bash script using the $1 variable. So in the count_lines.sh script, you can replace the filename variable with $1 as follows:

How to pass parameters or arguments to a shell script?

You can pass parameters or arguments to the file. Just the command for running the script normally by adding the value of the parameters directly to the script. Every parameter is a space-separated value to pass to the shell script.

How to store all command line arguments in a bash array?

You can store all command line arguments or parameter in a bash array as follows: array = ($ @) First, you need to find out length of an array: len = $ {#array ]}

How to print the value of a string argument in Bash?

In this Bash script, we created a function named “Hello.” Inside the body of the function, we will print a message, followed by “$1,” which represents the value of the string argument that will be passed to this function.


2 Answers

Just shift off the front ones as you're done with them; what's left will be in "$@".

This has the advantage of being compatible with all POSIX shells (the only extension used in the below is local, and it's a widespread one, even available in dash).

custom_scp() {
  local user port  # avoid polluting namespace outside your function
  port=$1; shift   # assign to a local variable, then pop off the argument list
  user=$1; shift   # repeat
  scp -P "$port" -r "$@" "${user}@myserver.com:~/"
}
like image 51
Charles Duffy Avatar answered Nov 23 '22 17:11

Charles Duffy


You can use array slice notation:

custom_scp() {
    local port=$1
    local user=$2
    local sources=("${@:3}")

    scp -P "$port" -r "${sources[@]}" "[email protected]:~/"
}

Quoting from the Bash manual:

${parameter:offset}
${parameter:offset:length}

If parameter is @, the result is length positional parameters beginning at offset.

like image 26
John Kugelman Avatar answered Nov 23 '22 15:11

John Kugelman