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
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.
$# 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.
"$@" 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.
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.
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:
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.
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 ]}
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.
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:~/"
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With